欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

15分鐘上手vue3.0(小結(jié))

 更新時間:2020年05月20日 09:17:40   作者:果糖醬  
這篇文章主要介紹了15分鐘上手vue3.0,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

Vue 3 還沒有正式發(fā)布,但是 Alpha 版本已經(jīng)發(fā)布了。

雖然官方還不推薦在生產(chǎn)環(huán)境中直接使用 Vue 3 ,但是提前學(xué)習(xí)總歸是有好處的。

嘴上喊著老子學(xué)不動了,雙手還是很誠實(shí)的打開了 Vue 3 文檔

創(chuàng)建項(xiàng)目

Vue 官方很貼心的提供了一個 github 倉庫,讓我們能快速體驗(yàn)Vue 3的新特性:

git clone https://github.com/vuejs/vue-next-webpack-preview.git vue3-start
cd vue3-start
npm install or yarn intall

開發(fā)環(huán)境準(zhǔn)備就緒后,啟動命令:

npm run dev

在瀏覽器中打開 http://127.0.0.1:8080 ,您可以看到一個簡單的計(jì)數(shù)器頁面:

打開 package.json,當(dāng)前使用的 vue 版本是:3.0.0-beta.2

Vue 3 新特性

Vue 3 的設(shè)計(jì)目標(biāo)是更快,更小,并更好的支持 TypeScript 。

一些新特性包括:

1、Composition API
2、Multiple root elements
3、Suspense
4、Multiple V-models
5、Reactivity
6、Teleport
7、Transition
8、Remove Filter
9、App configuration

1、Composition API

Vue 官方發(fā)布了 Composition API 的官方插件,使廣大用戶可以在 Vue2.x 中享受 Function Base 帶來的新體驗(yàn)。
而在 vue 3 中無需單獨(dú)安裝插件,開箱即用。

打開 App.vue,你會看到 setup()方法:

<template>
 <img src="./logo.png">
 <h1>Hello Vue 3!</h1>
 <button @click="inc">Clicked {{ count }} times.</button>
</template>

<script>
import { ref } from 'vue'

export default {
 setup() {
 const count = ref(0)
 const inc = () => {
  count.value++
 }

 return {
  count,
  inc
 }
 }
}
</script>

<style scoped>
img {
 width: 200px;
}
h1 {
 font-family: Arial, Helvetica, sans-serif;
}
</style>

Composition API 主要提供兩大好處:

1、清晰的代碼結(jié)構(gòu)
2、消除重復(fù)邏輯

<template>
 <div class="counter">
 <p>count: {{ count }}</p>
 <p>NewVal (count + 2): {{ countDouble }}</p>
 <button @click="inc">Increment</button>
 <button @click="dec">Decrement</button>
 <p> Message: {{ msg }} </p>
 <button @click="changeMessage()">Change Message</button>
 </div>
</template>
<script>
import { ref, computed, watch } from 'vue'
export default {
 setup() {
 /* ---------------------------------------------------- */
 let count = ref(0)
 const countDouble = computed(() => count.value * 2)
 watch(count, newVal => {
  console.log('count changed', newVal)
 })
 const inc = () => {
  count.value += 1
 }
 const dec = () => {
  if (count.value !== 0) {
  count.value -= 1
  }
 }
 /* ---------------------------------------------------- */
 let msg= ref('some text')
 watch(msg, newVal => {
  console.log('msg changed', newVal)
 })
 const changeMessage = () => {
  msg.value = "new Message"
 }
 /* ---------------------------------------------------- */
 return {
  count,
  inc,
  dec,
  countDouble,
  msg,
  changeMessage
 }
 }
}
</script>

如果你不喜歡使用 Composition API, 也可以繼續(xù)使用 2.x 的傳統(tǒng)方法:

<template>
 <div class="counter">
 <p>count: {{ count }}</p>
 <p>NewVal (count + 2): {{ countDouble }}</p>
 <button @click="inc">Increment</button>
 <button @click="dec">Decrement</button>
 <p> Message: {{ msg }} </p>
 <button @click="changeMessage()">Change Message</button>
 </div>
</template>
<script>
export default {
 data() {
 return {
  count: 0,
  msg: 'some message'
 }
 },
 computed: {
 countDouble() {
  return this.count*2
 }
 },
 watch: {
 count(newVal) {
  console.log('count changed', newVal)
 },
 msg(newVal) {
  console.log('msg changed', newVal)
 }
 },
 methods: {
  inc() {
  this.count += 1
 },
 dec() {
  if (this.count !== 0) {
  this.count -= 1
  }
 },
 changeMessage() {
  msg = "new Message"
 }
 }

}
</script>

上面兩段代碼在效果上市完全等價的

使用 Composition API 的好處:可以讓我們更好地組織代碼(state, methods, computed properties, watcher 等)。
隨著組件規(guī)模的增長,如何組織我們的業(yè)務(wù)代碼逐漸變成一個重要的問題,確保新進(jìn)的開發(fā)人員都可以輕松地理解代碼,而不需要花太多時間。

以前我們會使用 mixin 來復(fù)用代碼。然而,mixin 最大的痛點(diǎn)是,需要我們跟蹤不同組件中的狀態(tài)和方法,這往往會給開發(fā)帶來一定的心智負(fù)擔(dān),一不小心,mixin 可能會覆蓋組件中的現(xiàn)有狀態(tài)或方法。

使用 Composition API 讓代碼復(fù)用更加容易。

我們同樣可以抽取出重復(fù)功能的代碼:

// message.js
import { ref, watch } from "vue";
export function message() {
 let msg = ref(123);
 watch(msg, (newVal) => {
 console.log("msg changed", newVal);
 });
 const changeMessage = () => {
 msg.value = "new Message";
 };
 return { msg, changeMessage };
}

在其他組件中使用上面組件:

<template>
 <div class="counter">
 <p>count: {{ count }}</p>
 <p>NewVal (count + 2): {{ countDouble }}</p>
 <button @click="inc">Increment</button>
 <button @click="dec">Decrement</button>
 <p>Message: {{ msg }}</p>
 <button @click="changeMessage()">change message</button>
 </div>
</template>
<script>
import { ref, computed, watch } from 'vue'
import { message } from './common/message'
export default {
 setup() {
 let count = ref(0)
 const countDouble = computed(() => count.value * 2)
 watch(count, newVal => {
  console.log('count changed', newVal)
 })
 const inc = () => {
  count.value += 1
 }
 const dec = () => {
  if (count.value !== 0) {
  count.value -= 1
  }
 }
 let { msg, changeMessage } = message()
 return {
  count,
  msg,
  changeMessage,
  inc,
  dec,
  countDouble
 }
 }
}
</script>

2、Multiple root elements

在 Vue 2 中,tempalte 只能取一個根元素。即使我們只有兩個 <p> 標(biāo)記,我們也必須將它們包含在一個 <div> 標(biāo)記中:

<template>
 <div class="counter">
 <p> Count: {{ count }} </p>
 <button @click="increment"> Increment </button>
 <button @click="decrement"> Decrement</button>
 </div>
</template>

為了能編譯通過,我們通常會增加一個根節(jié)點(diǎn)。

這個設(shè)計(jì)確實(shí)很糟糕,我曾經(jīng)無數(shù)次吐槽過這個設(shè)計(jì)。因?yàn)闀聿槐匾拇a嵌套和縮進(jìn)。

幸好在 Vue 3 中取消了這一限制:
可以直接在<template></template>中使用任意數(shù)量的標(biāo)簽:

<template>
 <p> Count: {{ count }} </p>
 <button @click="increment"> Increment </button>
 <button @click="decrement"> Decrement </button>
</template>

用 VScode 打開模板時,看到一些 lint 錯誤,這是因?yàn)楣俜讲寮?eslint-plugin-vue 還沒有支持新的模板語法。

3、Suspense

Suspense 是一個 Vue 3 新特性。

通常前后端交互是一個異步的過程: 默認(rèn)我們提供一個加載中的動畫,當(dāng)數(shù)據(jù)返回時配合使用 v-if 來控制數(shù)據(jù)顯示。
Suspense 的出現(xiàn)大大簡化了這個過程:它提供了 default 和 fallback 兩種狀態(tài):

<template>
 <Suspense>
 <template #default>
  <div v-for="item in articleList" :key="item.id">
  <article>
   <h2>{{ item.title }}</h2>
   <p>{{ item.body }}</p>
  </article>
  </div>
 </template>
 <template #fallback>
  Articles loading...
 </template>
 </Suspense>
</template>
<script>
import axios from 'axios'
export default {
 async setup() {
 let articleList = await axios
  .get('https://jsonplaceholder.typicode.com/posts')
  .then(response => {
  console.log(response)
  return response.data
  })
 return {
  articleList
 }
 }
}
</script>

4、Multiple v-models

我們都知道 v-models 用于雙向數(shù)據(jù)綁定。一般用于與表單元素一起使用。有時我們會在自定義組件中使用它。
Vue 2 只允許在一個組件上使用一個 v-models。在 Vue 3 中,我們可以將任意數(shù)量的 v-model 綁定到我們的定制組件上:

<template>
 <survey-form v-model:name="name" v-model:age="age">
 {" "}
 </survey-form>
</template>
SurveyForm.vue:
<template>
 <div>
  <label>Name: </label>
  <input :value="name" @input="updateName($event.target.value)" />
  <label>Age: </label>
  <input :value="age" @input="updateAge($event.target.value)" />
 </div>
</template>
<script>
 export default {
  props: {
  name: String,
  age: Number
  },
  setup(props, { emit }) {
  const updateName = value => {
   emit('update:name', value)
  }
  const updateAge = value => {
   emit('update:age', +value)
  }
  return { updateName, updateAge }
  }
 }
</script>

5、Reactivity

Vue 2 的響應(yīng)式已經(jīng)非常棒了,但在少數(shù)情況下會存在一些問題:

<template>
 <div class="hello" @click="test">test {{list }} {{ myObj }}</div>
</template>
<script>
export default {
 name: "HelloWorld",
 data() {
 return {
  list: [1, 2],
  myObj: { name: "Preetish" }
 };
 },
 watch: {
 list: {
  handler: () => {
  console.log("watcher triggered");
  },
  deep: true
 }
 },
 methods: {
 test() {
  this.list[2] = 4;
  this.myObj.last = "HS";
  delete this.myObj.name;
 }
 }
};
</script>

我們發(fā)現(xiàn)通過this.list下標(biāo)來修改元素,并不會觸發(fā) wacher 監(jiān)聽函數(shù),為了達(dá)到目的,我們不得不使用 vue.set() 或 vue.delete() 這些方法。

而在 vue 3 中,我們不需要借助其他 API:

export default {
 setup() {
 let list = ref([1, 2]);
 let myObj = ref({ name: "Preetish" });
 function myFun() {
  list.value[3] = 3;
  myObj.value.last = "HS";
  delete myObj.value.name;
 }
 return { myFun, list, myObj };
 },
};

6、Portals

Portals 提供了一種將組件中渲染到頁面任意一個 DOM 節(jié)點(diǎn)中的能力。在 Vue 2 中,利用一個 portal-vue 的第三方插件來做到這一點(diǎn)。

在 vue 3 中直接使用:

<Teleport to="#modal-layer">
 <div class="modal">hello</div>
</Teleport>
<Teleport> 是 vue3 中提供特定的標(biāo)簽用于創(chuàng)建一個 Portals。
<Teleport> </Teleport>中間出現(xiàn)的內(nèi)容會出現(xiàn)在 to 指定的節(jié)點(diǎn)中:
<div id="modal-target"></div>

目前為止,<Teleport>在 Alpha 版本中并不能使用

7、Transition

之前我在使用 v-enter-active, v-enter, v-enter-to 這些個狀態(tài)時搞的暈頭轉(zhuǎn)向。
現(xiàn)在 Vue 3 從命名上更直觀了: v-enter 變成了 v-enter-from,v-leave 變成 v-leave-from。

8、Remove Filter

Vue 3 拋棄了 Filter 的用法,更推薦使用計(jì)算屬性或方法來實(shí)現(xiàn):

<!-- vue 2.x -->
{{ date | format }}

<!-- vue 3.0 -->
{{ format(date) }}

9、App configration

在 Vue 2 中,如果想使用 use(), mixin() , directive() 等方法需要配合全局 Vue 對象:

import Vue from "vue";
import App from "./App";

Vue.use(/* ... */);
Vue.mixin(/* ... */);
Vue.component(/* ... */);
Vue.directive(/* ... */);

new Vue({
 el: "#app",
 template: "<App/>",
 components: {
  App,
 },
});

在 Vue 3 中, 改成了 createApp 返回的 Vue 實(shí)例:

import { createApp } from "vue";
import App from "./App.vue";

const app = createApp(App);

app.use(/* ... */);
app.mixin(/* ... */);
app.component(/* ... */);
app.directive(/* ... */);

app.mount("#app");

結(jié)束語

總之Vue 3 通過提供一種簡單的方式來組織和共享代碼,并提供強(qiáng)大的 TypeScript 支持,新的代碼組織方式會對未來的應(yīng)用開發(fā)產(chǎn)生重大影響。

同時一些其它的特性,如 Suspense,多個 v-models 等也會給開發(fā)帶來巨大的便利。

同時性能更快,體積更小。它是如何做到的請參考我寫的另一篇文章:Vue.js 作者:關(guān)于 Vue3.0 背后的故事

 到此這篇關(guān)于15分鐘上手vue3.0(小結(jié))的文章就介紹到這了,更多相關(guān)vue3.0入門內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue中watch和computed的區(qū)別與使用方法

    vue中watch和computed的區(qū)別與使用方法

    這篇文章主要給大家介紹了關(guān)于vue中watch和computed的區(qū)別與使用方法的相關(guān)資料,文中通過實(shí)例代碼結(jié)束的非常詳細(xì),對大家學(xué)習(xí)或者使用Vue具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • vue中實(shí)現(xiàn)滾動加載更多的示例

    vue中實(shí)現(xiàn)滾動加載更多的示例

    下面小編就為大家?guī)硪黄獀ue中實(shí)現(xiàn)滾動加載更多的示例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Vue利用mockjs編寫假數(shù)據(jù)并應(yīng)用的問題記錄

    Vue利用mockjs編寫假數(shù)據(jù)并應(yīng)用的問題記錄

    這篇文章主要介紹了Vue利用mockjs編寫假數(shù)據(jù)并應(yīng)用,本文通過實(shí)例代碼給大家詳細(xì)講解,對Vue?mockjs數(shù)據(jù)相關(guān)知識感興趣的朋友跟隨小編一起看看吧
    2022-12-12
  • vue動態(tài)加載本地圖片的處理方法

    vue動態(tài)加載本地圖片的處理方法

    最近遇到了個問題,用v-bind動態(tài)綁定img的src,圖片加載不出來,所以下面這篇文章主要給大家介紹了關(guān)于vue動態(tài)加載本地圖片的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • npm install卡在“sill idealTree buildDeps“問題的兩種解決方法

    npm install卡在“sill idealTree buildDeps“問題的兩種解

    本文主要介紹了npm install卡在“sill idealTree buildDeps“問題的兩種解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-03-03
  • Vue不能檢測到數(shù)據(jù)變化的幾種情況說明

    Vue不能檢測到數(shù)據(jù)變化的幾種情況說明

    這篇文章主要介紹了Vue不能檢測到數(shù)據(jù)變化的幾種情況說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • VUE前端從后臺請求過來的數(shù)據(jù)進(jìn)行轉(zhuǎn)換數(shù)據(jù)結(jié)構(gòu)操作

    VUE前端從后臺請求過來的數(shù)據(jù)進(jìn)行轉(zhuǎn)換數(shù)據(jù)結(jié)構(gòu)操作

    VUE前端從后臺請求過來的數(shù)據(jù)進(jìn)行轉(zhuǎn)換數(shù)據(jù)結(jié)構(gòu)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Xx-vue自定義指令實(shí)現(xiàn)點(diǎn)擊水波紋漣漪效果

    Xx-vue自定義指令實(shí)現(xiàn)點(diǎn)擊水波紋漣漪效果

    這篇文章主要為大家介紹了Xx-vue自定義指令實(shí)現(xiàn)點(diǎn)擊水波紋漣漪效果,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • vue3.0響應(yīng)式函數(shù)原理詳細(xì)

    vue3.0響應(yīng)式函數(shù)原理詳細(xì)

    這篇文章主要介紹了vue3.0響應(yīng)式函數(shù)原理,Vue3的響應(yīng)式系統(tǒng)可以監(jiān)聽動態(tài)添加的屬性還可以監(jiān)聽屬性的刪除操作,以及數(shù)組的索引以及l(fā)ength屬性的修改操作。另外Vue3的響應(yīng)式系統(tǒng)還可以作為模塊單獨(dú)使用。下面更多介紹,需要的小伙伴可以才可以參考一下
    2022-02-02
  • Vue3中引用本地圖片路徑的方法詳解

    Vue3中引用本地圖片路徑的方法詳解

    這篇文章主要為大家詳細(xì)介紹了Vue3中引用本地圖片路徑的常用方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-03-03

最新評論