[vue3]vue3新特性 - 图1

来自拉勾 [vue3]vue3新特性 - 图2

名词解释

SFC:single file component

RFCs: request for comments

搭建

  • vite(推荐)
  • vue-cli4
  1. yarn create vite-app vue3-demo
  2. yarn create v1.22.4
  3. [1/4] 🔍 Resolving packages...
  4. [2/4] 🚚 Fetching packages...
  5. [3/4] 🔗 Linking dependencies...
  6. [4/4] 🔨 Building fresh packages...
  7. success Installed "create-vite-app@1.20.0" with binaries:
  8. - create-vite-app
  9. - cva
  10. Scaffolding project in /Users/zc/workspace/vue3-demo...
  11. Done. Now run:
  12. cd vue3-demo
  13. npm install (or `yarn`)
  14. npm run dev (or `yarn dev`)
  15. Done in 15.54s.

挂载

⚠️ 与vue2有区别

  1. import { createApp } from 'vue'
  2. import App from './App.vue'
  3. createApp(App).mount('#app')

vue2.6的挂在方式

  1. import Vue from 'vue'
  2. new Vue({
  3. el: '#app',
  4. router,
  5. store,
  6. components: { App },
  7. template: '<App/>'
  8. })

vue2.5

  1. new Vue({
  2. // eslint-disable-line
  3. render: (h) => h(App)
  4. }).$mount('#app');

组件

ui组件库 element-plus

复合组件

reactive and isReactive

  1. <template>
  2. <div>
  3. {{ count }}
  4. </div>
  5. </template>
  6. <script>
  7. import { reactive } from 'vue';
  8. export default {
  9. name: 'Composition',
  10. setup () {
  11. const state = reactive({count: 1});
  12. console.log(isReactive(state)) // true
  13. console.log(isReactive({ count: 1})) // false
  14. return state;
  15. }
  16. }
  17. </script>

readonly and isReadonly

  1. import { reactive, isReactive, readonly, isReadonly } from 'vue';
  2. export default {
  3. name: 'Composition',
  4. setup () {
  5. const state = reactive({count: 1});
  6. let copy = readonly(state);
  7. console.log(isReactive(copy)) // true
  8. console.log(isReadonly(copy)) // true
  9. console.log(isReadonly(state)) // false
  10. console.log(isReadonly({count: 1})) // false
  11. return state;
  12. }
  13. }

isProxy

  1. import { reactive, isReactive, readonly, isReadonly, isProxy } from 'vue';
  2. export default {
  3. name: 'Composition',
  4. setup () {
  5. const state = reactive({count: 1});
  6. let copy = readonly(state);
  7. console.log(isProxy(state)) // true
  8. console.log(isProxy(copy)) // true
  9. console.log(isProxy({count: 1})) // false
  10. return state;
  11. }
  12. }

源码

  1. export function isProxy(value: unknown): boolean {
  2. return isReactive(value) || isReadonly(value)
  3. }

声明周期

beforeDestory、destoryed
钩子函数名称替换为beforeUnmount、unmounted

参考:

中文文档

vue-router for vue3

vuex for vue3

https://v3.vuejs.org/guide/typescript-support.html#official-declaration-in-npm-packages

TODO:

【课程】Vue 3响应式原理 (Vue 3 Reactivity) - Vue Mastery

参考:
vue-3-reactivity https://www.bilibili.com/video/BV1SZ4y1x7a9?p=1

Vue 3 Deep Dive with Evan You https://www.bilibili.com/video/BV1rC4y187Vw

https://www.bilibili.com/video/BV1jz411z769?from=search&seid=7130680114124910206

https://composition-api.vuejs.org/zh/api.html#%E5%93%8D%E5%BA%94%E5%BC%8F%E7%B3%BB%E7%BB%9F-api