?? ?? 來嘍,來嘍,他真的來嘍,Vue3.0 ?。。。?/h4>官方文檔( 中文版 ):https://vue3js.cn/docs/zh/
創(chuàng)建一個Vue3.0的項目
? ? 1、npm init vite-app vue3.0
? ? 2、cd?vue3.0
? ? 3、npm install
? ? 4、npm run dev
官方文檔( 中文版 ):https://vue3js.cn/docs/zh/
創(chuàng)建一個Vue3.0的項目
? ? 1、npm init vite-app vue3.0
? ? 2、cd?vue3.0
? ? 3、npm install
? ? 4、npm run dev

? ? 此時,項目已經(jīng)創(chuàng)建成功!?。。?/p>
接下來,我們先來體驗一下Vue3.0的新東西
??????Object.defineProperty => Proxy
????????重構(gòu)了虛擬
????????DOMOptionApi => Composition API
1、setup()? ? ??beforeCreate 、created 之前執(zhí)行
????????setup() 函數(shù)是 vue3 中,專門為組件提供的新屬性。它為我們使用 vue3 的 Composition API 新特性提供了統(tǒng)一的入口, vue3取消了beforeCreate 、created兩個鉤子,統(tǒng)一用setup代替, 該函數(shù)相當(dāng)于一個生命周期函數(shù),vue中過去的data,methods,watch,computed等全部都用對應(yīng)的新增api寫在setup()函數(shù)中
2、ref()? 聲明單一基礎(chǔ)數(shù)據(jù)類型? 返回值是響應(yīng)式對象
? ???創(chuàng)建一個響應(yīng)式的數(shù)據(jù)對象,ref() 函數(shù)調(diào)用的返回值是一個對象,這個對象上只包含一個 value 屬性, 只在setup函數(shù)內(nèi)部訪問ref函數(shù)需要加.value
3、reactive()? ?聲明單一對象? (注意:解構(gòu)會破壞代理proxy即雙向綁定)
????reactive() 函數(shù)接收一個普通對象,返回一個響應(yīng)式的數(shù)據(jù)對象, 想要使用創(chuàng)建的響應(yīng)式數(shù)據(jù)也很簡單,創(chuàng)建出來之后,在setup中return出去,直接在template中調(diào)用即可; 在reactive()函數(shù)中訪問ref()函數(shù)創(chuàng)建的對象不用.value
4、isRef()? ?判斷是否是通過ref()函數(shù)創(chuàng)建出來的對象
5、toRefs()? ?從組合函數(shù)返回反應(yīng)對象
? ??toRefs() 函數(shù)可以將 reactive() 創(chuàng)建出來的響應(yīng)式對象,轉(zhuǎn)換為普通的對象,只不過,這個對象上的每個屬性節(jié)點,都是 ref() 類型的響應(yīng)式數(shù)據(jù)
6、computed()? ??計算屬性?可創(chuàng)建只讀,和可讀可寫兩種
? ? 1)? 只讀? ? ? ? ? ?let fullName = computed(() => msg.value + '~' + name) ? ?
? ? 2)可讀可寫? ??let fullName2 = computed({ get: () => number.value + state.step, set: (v) => number.value = v })
7、watch()
? ??watch 函數(shù)用來偵聽特定的數(shù)據(jù)源,并在回調(diào)函數(shù)中執(zhí)行副作用。默認(rèn)情況是懶執(zhí)行的,也就是說僅在偵聽的源數(shù)據(jù)變更時才執(zhí)行回調(diào)。
? ? 1)?監(jiān)聽 reactive( )聲明的數(shù)據(jù)
? ? ? ? 單數(shù)據(jù)監(jiān)聽:? ? watch( ( )=> reactiveV.name, (n, o)=>{ })
? ? ? ? 多數(shù)據(jù)監(jiān)聽:? ? ?watch([( )=> reactive.name, ( )=> reactive.sex], ([name, sex], [oldName, oldSex])=> { })
? ? 2)? ?監(jiān)聽 ref( )聲明的數(shù)據(jù)
? ? ? ? ?單數(shù)據(jù)監(jiān)聽:? ? ?watch(age,(n, o)=>{ })
? ???????多數(shù)據(jù)監(jiān)聽:? ? ?watch([age, weight], ([newAge, newWeight], [oldAge, oldWeight])=> { })
? ? 3)取消監(jiān)聽
? ? ? ? ? const stop = watch(age,(n, o)=>{ })
? ? ? ? ? setTimeout(() => { stop()?}, 1000)? ? ? ? ? ? ??// 一秒后取消對age的監(jiān)聽
8、LifeCycle Hooks(新的生命周期)
? ? ? ? onBeforeMount(() => { console.log('onBeforeMount') })?
? ??????onMounted(() => { console.log('onMounted') })?
? ??????onBeforeUpdate(() => {? ?? ?console.log('onBeforeUpdate') })?
? ??????onUpdated(() => { console.log('onUpdated') })?
? ??????onBeforeUnmount(() => { console.log('onBeforeUnmount') })
? ??????onUnmounted(() => { console.log('onUnmounted') })
9、Template?refs? ??通過refs 來回去真實dom元素
????????<h3 ref="h3">hello</h3>
? ? ? ? 想要得到h3的dom 在2.x中?this.$refs.h3? 在3.0中如下:
? ? ? ? ? 1)在setUp函數(shù)中聲明?let h3 = ref(null),并return出去h3
? ? ? ? ? 2)在onMounted()函數(shù)中打印 h3.value你會得到(<h3 data-v-c5362648="">hello</h3>)
10、global?Properties??vue3.0 的全局配置
? ??????Vue 2.x? 通過 Vue.prototype擴展
? ? ? ? vue3.0 通過app.config.globalProperties.$api = 'http://www.baidu.com'
? ? ? ? 在setUp函數(shù)中通過 const { ctx } = getCurrentInstance()? ? ? ctx.$api ( http://www.baidu.com )
11、Suspense
? ??異步組件在默認(rèn)情況下是可掛起的。這意味著如果它在父鏈中有一個?<Suspense>,它將被視為該?<Suspense>?的異步依賴。在這種情況下,加載狀態(tài)將由?<Suspense>?控制,組件自身的加載、錯誤、延遲和超時選項將被忽略。異步組件可以選擇退出?Suspense?控制,并通過在其選項中指定?suspensable:false,讓組件始終控制自己的加載狀態(tài)。
??:1、Vue3.0template可以有多個根結(jié)點
? ? ? ? 2、創(chuàng)建組件需要用defineComponent包裹。
看看Vuex的使用
? ? 在setup()函數(shù)中使用store中的state,commits,actions
? ? 首先,獲取ctx? ? const { ctx } = getCurrentInstance()
? ? 此時ctx.$store就是我們想要的store ,OK,想咋用就咋用了

暫時更新這么多,后續(xù)還會增加,敬請期待?。。。?/p>