av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

10分鐘讓你快速上手Vue3

經(jīng)過了漫長地迭代,Vue 3.0 終于在上 2020-09-18 發(fā)布了,帶了翻天覆地的變化,使用了 Typescript 進行了大規(guī)模的重構(gòu),帶來了 Composition API RFC 版本,類似 React Hook 一樣的寫 Vue,可以自定義自己的 hook ,讓使用者更加的靈活,接下來總結(jié)一下 vue 3.0 帶來的部分新特性。

創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、燈塔網(wǎng)站定制設(shè)計、自適應品牌網(wǎng)站建設(shè)、H5網(wǎng)站設(shè)計、成都做商城網(wǎng)站、集團公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應式網(wǎng)頁設(shè)計等建站業(yè)務,價格優(yōu)惠性價比高,為燈塔等各大城市提供網(wǎng)站開發(fā)制作服務。

  1. setup()
  2. ref()
  3. reactive()
  4. isRef()
  5. toRefs()
  6. computed()
  7. watch()
  8. LifeCycle Hooks(新的生命周期)
  9. Template refs
  10. globalProperties
  11. Suspense

Vue2 與 Vue3 的對比

  • 對 TypeScript 支持不友好(所有屬性都放在了 this 對象上,難以推倒組件的數(shù)據(jù)類型)
  • 大量的 API 掛載在 Vue 對象的原型上,難以實現(xiàn) TreeShaking。
  • 架構(gòu)層面對跨平臺 dom 渲染開發(fā)支持不友好
  • CompositionAPI。愛 ReactHook 啟發(fā)
  • 更方便的支持了 jsx
  • Vue 3 的 Template 支持多個根標簽,Vue 2 不支持
  • 對虛擬 DOM 進行了重寫、對模板的編譯進行了優(yōu)化操作...

一、setup 函數(shù)

setup() 函數(shù)是 vue3 中,專門為組件提供的新屬性。它為我們使用 vue3 的 Composition API 新特性提供了統(tǒng)一的入口, setup 函數(shù)會在 beforeCreate 之后、created 之前執(zhí)行, vue3 也是取消了這兩個鉤子,統(tǒng)一用 setup 代替, 該函數(shù)相當于一個生命周期函數(shù),vue 中過去的 data,methods,watch 等全部都用對應的新增 api 寫在 setup()函數(shù)中

 
 
 
  1. setup(props, context) { 
  2.     context.attrs 
  3.     context.slots 
  4.     context.parent 
  5.     context.root 
  6.     context.emit 
  7.     context.refs 
  8.  
  9.     return { 
  10.  
  11.     } 
  12.   } 
  • props: 用來接收 props 數(shù)據(jù)
  • context 用來定義上下文, 上下文對象中包含了一些有用的屬性,這些屬性在 vue 2.x 中需要通過 this 才能訪問到, 在 setup() 函數(shù)中無法訪問到 this,是個 undefined
  • 返回值: return {}, 返回響應式數(shù)據(jù), 模版中需要使用的函數(shù)

二、reactive 函數(shù)

reactive() 函數(shù)接收一個普通對象,返回一個響應式的數(shù)據(jù)對象, 想要使用創(chuàng)建的響應式數(shù)據(jù)也很簡單,創(chuàng)建出來之后,在 setup 中 return 出去,直接在 template 中調(diào)用即可

 
 
 
  1.  
  2.  
  3.  
  4.  import { defineComponent, defineAsyncComponent } from "vue"; 
  5.  const MyComponent = defineAsyncComponent(() => import('./Component')); 
  6.  
  7. export default defineComponent({ 
  8.    components: { 
  9.      MyComponent 
  10.    }, 
  11.    setup() { 
  12.      return {} 
  13.    } 
  14. }) 
  15.  
  16.  
  17.  

十二、vue 3.x 完整組件模版結(jié)構(gòu)

一個完成的 vue 3.x 完整組件模版結(jié)構(gòu)包含了:組件名稱、 props、components、setup(hooks、computed、watch、methods 等)

 
 
 
  1.  
  2.  
  3.  
  4. import { computed, defineComponent, getCurrentInstance, onMounted, PropType, reactive, ref, toRefs } from 'vue'; 
  5.  
  6. interface IState { 
  7.   count: 0, 
  8.   name: string, 
  9.   list: Array 
  10.  
  11. export default defineComponent({ 
  12.   name: 'demo', 
  13.   // 父組件傳子組件參數(shù) 
  14.   props: { 
  15.     name: { 
  16.       type: String as PropType
  17.       default: 'vue3.x' 
  18.     }, 
  19.     list: { 
  20.       type: Array as PropType
  21.       default: () => [] 
  22.     } 
  23.   }, 
  24.   components: { 
  25.     /// TODO 組件注冊 
  26.   }, 
  27.   emits: ["emits-name"], // 為了提示作用 
  28.   setup (props, context) { 
  29.     console.log(props.name) 
  30.     console.log(props.list) 
  31.  
  32.  
  33.     const state = reactive({ 
  34.       name: 'vue 3.0 組件', 
  35.       count: 0, 
  36.       list: [ 
  37.         { 
  38.           name: 'vue', 
  39.           id: 1 
  40.         }, 
  41.         { 
  42.           name: 'vuex', 
  43.           id: 2 
  44.         } 
  45.       ] 
  46.     }) 
  47.  
  48.     const a = computed(() => state.name) 
  49.  
  50.     onMounted(() => { 
  51.  
  52.     }) 
  53.  
  54.     function handleClick () { 
  55.       state.count ++ 
  56.       // 調(diào)用父組件的方法 
  57.       context.emit('emits-name', state.count) 
  58.     } 
  59.  
  60.     return { 
  61.       ...toRefs(state), 
  62.       handleClick 
  63.     } 
  64.   } 
  65. }); 
  66.  
  67.  
  68.  
  69.  
  70.  
  71. import { computed, defineComponent, getCurrentInstance, onMounted, PropType, reactive, ref, toRefs } from 'vue'; 
  72.  
  73. interface IState { 
  74.   count: 0, 
  75.   name: string, 
  76.   list: Array 
  77.  
  78. export default defineComponent({ 
  79.   name: 'demo', 
  80.   // 父組件傳子組件參數(shù) 
  81.   props: { 
  82.     name: { 
  83.       type: String as PropType
  84.       default: 'vue3.x' 
  85.     }, 
  86.     list: { 
  87.       type: Array as PropType
  88.       default: () => [] 
  89.     } 
  90.   }, 
  91.   components: { 
  92.     /// TODO 組件注冊 
  93.   }, 
  94.   emits: ["emits-name"], // 為了提示作用 
  95.   setup (props, context) { 
  96.     console.log(props.name) 
  97.     console.log(props.list) 
  98.  
  99.  
  100.     const state = reactive({ 
  101.       name: 'vue 3.0 組件', 
  102.       count: 0, 
  103.       list: [ 
  104.         { 
  105.           name: 'vue', 
  106.           id: 1 
  107.         }, 
  108.         { 
  109.           name: 'vuex', 
  110.           id: 2 
  111.         } 
  112.       ] 
  113.     }) 
  114.  
  115.     const a = computed(() => state.name) 
  116.  
  117.     onMounted(() => { 
  118.  
  119.     }) 
  120.  
  121.     function handleClick () { 
  122.       state.count ++ 
  123.       // 調(diào)用父組件的方法 
  124.       context.emit('emits-name', state.count) 
  125.     } 
  126.  
  127.     return { 
  128.       ...toRefs(state), 
  129.       handleClick 
  130.     } 
  131.   } 
  132. }); 
  133.  
  134. vue 3 的生態(tài)

    • 官網(wǎng)
    • 源碼
    • vite 構(gòu)建器
    • 腳手架:https://cli.vuejs.org/
    • vue-router-next
    • vuex4.0

    UI 組件庫

    • vant2.x
    • Ant Design of Vue 2.x
    • element-plus

    網(wǎng)站題目:10分鐘讓你快速上手Vue3
    分享鏈接:http://uogjgqi.cn/article/dhepisc.html
    掃二維碼與項目經(jīng)理溝通

    我們在微信上24小時期待你的聲音

    解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流