溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么上手vue3.0

發(fā)布時間:2020-07-23 11:02:05 來源:億速云 閱讀:148 作者:小豬 欄目:web開發(fā)

這篇文章主要講解了怎么上手vue3.0,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。

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

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

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

創(chuàng)建項目

Vue 官方很貼心的提供了一個 github 倉庫,讓我們能快速體驗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)境準備就緒后,啟動命令:

npm run dev

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

怎么上手vue3.0

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

Vue 3 新特性

Vue 3 的設(shè)計目標是更快,更小,并更好的支持 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 帶來的新體驗。
而在 vue 3 中無需單獨安裝插件,開箱即用。

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

<template>
 <img src="./logo.png">
 <h2>Hello Vue 3!</h2>
 <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;
}
h2 {
 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ù)代碼逐漸變成一個重要的問題,確保新進的開發(fā)人員都可以輕松地理解代碼,而不需要花太多時間。

以前我們會使用 mixin 來復(fù)用代碼。然而,mixin 最大的痛點是,需要我們跟蹤不同組件中的狀態(tài)和方法,這往往會給開發(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> 標記,我們也必須將它們包含在一個 <div> 標記中:

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

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

這個設(shè)計確實很糟糕,我曾經(jīng)無數(shù)次吐槽過這個設(shè)計。因為會帶來不必要的代碼嵌套和縮進。

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

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

用 VScode 打開模板時,看到一些 lint 錯誤,這是因為官方插件 eslint-plugin-vue 還沒有支持新的模板語法。

3、Suspense

Suspense 是一個 Vue 3 新特性。

通常前后端交互是一個異步的過程: 默認我們提供一個加載中的動畫,當(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>
   <h3>{{ item.title }}</h3>
   <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下標來修改元素,并不會觸發(fā) wacher 監(jiān)聽函數(shù),為了達到目的,我們不得不使用 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é)點中的能力。在 Vue 2 中,利用一個 portal-vue 的第三方插件來做到這一點。

在 vue 3 中直接使用:

<Teleport to="#modal-layer">
 <div class="modal">hello</div>
</Teleport>
<Teleport> 是 vue3 中提供特定的標簽用于創(chuàng)建一個 Portals。
<Teleport> </Teleport>中間出現(xiàn)的內(nèi)容會出現(xiàn)在 to 指定的節(jié)點中:
<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 的用法,更推薦使用計算屬性或方法來實現(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 實例:

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

const app = createApp(App);

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

app.mount("#app");

看完上述內(nèi)容,是不是對怎么上手vue3.0有進一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI