最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

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

Vue 官方很貼心的提供了一個(gè) github 倉(cāng)庫(kù),讓我們能快速體驗(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

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

npm run dev

在瀏覽器中打開(kāi) http://127.0.0.1:8080 ,您可以看到一個(gè)簡(jiǎn)單的計(jì)數(shù)器頁(yè)面:

打開(kāi) 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 帶來(lái)的新體驗(yàn)。
而在 vue 3 中無(wú)需單獨(dú)安裝插件,開(kāi)箱即用。

打開(kāi) App.vue,你會(huì)看到 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>

上面兩段代碼在效果上市完全等價(jià)的

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

以前我們會(huì)使用 mixin 來(lái)復(fù)用代碼。然而,mixin 最大的痛點(diǎn)是,需要我們跟蹤不同組件中的狀態(tài)和方法,這往往會(huì)給開(kāi)發(fā)帶來(lái)一定的心智負(fù)擔(dān),一不小心,mixin 可能會(huì)覆蓋組件中的現(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 只能取一個(gè)根元素。即使我們只有兩個(gè) <p> 標(biāo)記,我們也必須將它們包含在一個(gè) <div> 標(biāo)記中:

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

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

這個(gè)設(shè)計(jì)確實(shí)很糟糕,我曾經(jīng)無(wú)數(shù)次吐槽過(guò)這個(gè)設(shè)計(jì)。因?yàn)闀?huì)帶來(lái)不必要的代碼嵌套和縮進(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 打開(kāi)模板時(shí),看到一些 lint 錯(cuò)誤,這是因?yàn)楣俜讲寮?eslint-plugin-vue 還沒(méi)有支持新的模板語(yǔ)法。

3、Suspense

Suspense 是一個(gè) Vue 3 新特性。

通常前后端交互是一個(gè)異步的過(guò)程: 默認(rèn)我們提供一個(gè)加載中的動(dòng)畫,當(dāng)數(shù)據(jù)返回時(shí)配合使用 v-if 來(lái)控制數(shù)據(jù)顯示。
Suspense 的出現(xiàn)大大簡(jiǎn)化了這個(gè)過(guò)程:它提供了 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ù)綁定。一般用于與表單元素一起使用。有時(shí)我們會(huì)在自定義組件中使用它。
Vue 2 只允許在一個(gè)組件上使用一個(gè) 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ù)情況下會(huì)存在一些問(wèn)題:

<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)通過(guò)this.list下標(biāo)來(lái)修改元素,并不會(huì)觸發(fā) wacher 監(jiān)聽(tīng)函數(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 提供了一種將組件中渲染到頁(yè)面任意一個(gè) DOM 節(jié)點(diǎn)中的能力。在 Vue 2 中,利用一個(gè) portal-vue 的第三方插件來(lái)做到這一點(diǎn)。

在 vue 3 中直接使用:

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

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

7、Transition

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

8、Remove Filter

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

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

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

9、App configration

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

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é)束語(yǔ)

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

同時(shí)一些其它的特性,如 Suspense,多個(gè) v-models 等也會(huì)給開(kāi)發(fā)帶來(lái)巨大的便利。

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

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

相關(guān)文章

  • vue.js,ajax渲染頁(yè)面的實(shí)例

    vue.js,ajax渲染頁(yè)面的實(shí)例

    下面小編就為大家分享一篇vue.js,ajax渲染頁(yè)面的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • 在Vue中實(shí)現(xiàn)文件預(yù)覽與打印的代碼示例

    在Vue中實(shí)現(xiàn)文件預(yù)覽與打印的代碼示例

    在Vue應(yīng)用中,有時(shí)我們需要實(shí)現(xiàn)文件預(yù)覽和打印的功能,比如,我們可能需要預(yù)覽并打印PDF文件、圖片文件等,本文將介紹如何在Vue中實(shí)現(xiàn)文件預(yù)覽和打印的功能,并提供相應(yīng)的代碼示例
    2023-06-06
  • vue3.0翻牌數(shù)字組件使用方法詳解

    vue3.0翻牌數(shù)字組件使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了vue3.0翻牌數(shù)字組件使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Vue.js進(jìn)階知識(shí)點(diǎn)總結(jié)

    Vue.js進(jìn)階知識(shí)點(diǎn)總結(jié)

    給大家分享了關(guān)于Vue.js想成為高手的5個(gè)總要知識(shí)點(diǎn),需要的朋友可以學(xué)習(xí)下。
    2018-04-04
  • Vue核心概念Getter的使用方法

    Vue核心概念Getter的使用方法

    今天小編就為大家分享一篇關(guān)于Vue核心概念Getter的使用方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • vue3給動(dòng)態(tài)渲染的組件添加ref的解決方案

    vue3給動(dòng)態(tài)渲染的組件添加ref的解決方案

    ref和reactive一樣,也是用來(lái)實(shí)現(xiàn)響應(yīng)式數(shù)據(jù)的方法,下面這篇文章主要給大家介紹了關(guān)于vue3給動(dòng)態(tài)渲染的組件添加ref的解決方案,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • vue-infinite-loading2.0 中文文檔詳解

    vue-infinite-loading2.0 中文文檔詳解

    本篇文章主要介紹了vue-infinite-loading2.0 中文文檔詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • vue3中使用ref獲取dom的操作代碼

    vue3中使用ref獲取dom的操作代碼

    ref在我們開(kāi)發(fā)項(xiàng)目當(dāng)中很重要的,在?Vue?中使用?ref?可以提高代碼的可讀性和維護(hù)性,因?yàn)樗苯訕?biāo)識(shí)出了組件中需要操作的具體元素或組件實(shí)例,本文我將給大家?guī)?lái)的是vue3中用ref獲取dom的操作,文中有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下
    2024-06-06
  • 使用element upload上傳組件如何去掉刪除按鈕

    使用element upload上傳組件如何去掉刪除按鈕

    這篇文章主要介紹了使用element upload上傳組件如何去掉刪除按鈕
    2023-07-07
  • vue中實(shí)現(xiàn)回車鍵登錄功能

    vue中實(shí)現(xiàn)回車鍵登錄功能

    這篇文章主要介紹了vue中實(shí)現(xiàn)回車鍵登錄功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-02-02

最新評(píng)論

永安市| 行唐县| 汶川县| 兖州市| 来凤县| 绍兴县| 左贡县| 兴和县| 珠海市| 临泉县| 融水| 托里县| 响水县| 合江县| 罗山县| 清徐县| 铁岭县| 江西省| 新乡县| 浦县| 瑞丽市| 东方市| 汝阳县| 拜泉县| 鄂伦春自治旗| 平山县| 额尔古纳市| 当涂县| 连城县| 徐州市| 肃宁县| 阜康市| 祁连县| 云浮市| 无棣县| 漠河县| 张家口市| 高雄县| 龙里县| 建德市| 金溪县|