vue3.0 支持组件中使用多个根节点,能够减小节点层级深度,也但愿开发者可以明确语义javascript
vue3.0 删除了过滤器(filters),将不在支持,官方建议使用计算属性(computed)替换过滤器html
vue3.0 暴露出不少 API 供开发者使用,能够根据需求,将所须要的 API 从 Vue 中导入。考虑到 tree-shaking,利用了 import 和 export 的语法,实现了按需打包模块的功能vue
vue3.0 中新增了一个新的全局 API createApp,调用 createApp 返回一个应用实例,该应用实例暴露出来全局 API(vue3.0 将能够全局改变 Vue 行为的 API 从原来的 Vue 构造函数上转移到了实例上了)java
//vue2.0建立import Vue from 'vue'import App from './App.vue' Vue.config.productionTip = false new Vue({ render: h => h(App), }).$mount('#app') //vue3.0建立import { createApp } from 'vue'import App from './App.vue' createApp(App).mount('#app');复制代码
Object.defineProperty 只能监听到属性的读写,而 Proxy 除读写外还能够监听属性的删除,方法的调用等react
Vue 组合式 API:composition-api.vuejs.org/zh/api.html…git
setup 函数是一个新的组件选项,做为在组件内使用 Composition API 的入口函数,变量、方法都定义在该函数中github
<template> <div> {{ count }} {{ obj.count }} </div></template><script>//引入须要的APIimport { ref, reactive } from 'vue'export default { name: 'home', setup() { const count = ref(0) export let obj = reactive({ count: 0 }) //经过return暴露出去,供template中使用 return { count, obj } } }</script><style scoped> </style>复制代码
使用 <script setup>vue-router
<template> <div> {{ count }} {{ obj.count }} </div></template><script setup>//引入须要的APIimport { ref, reactive } from 'vue'//经过export暴露出去,供template中使用export let num = ref(0)export let obj = reactive({ count: 0 })</script><style scoped> </style>复制代码
reactive 接收一个普通对象而后返回该普通对象的响应式代理,建立一个响应式的数据对象vue-cli
<template> <div> {{ obj.count }} </div></template><script>//引入须要的APIimport { reactive } from 'vue'export default { name: 'home', setup() { //经过reactive定义响应式对象 const obj = reactive({ count: 0 }) //经过return暴露出去,供template中使用 return { obj } } }</script>复制代码
ref 接受一个参数值并返回一个响应式且可改变的 ref 对象。ref 对象拥有一个指向内部值的单一属性 .valuenpm
<template> <div> {{ count }} </div></template><script>//引入须要的APIimport { ref } from 'vue'export default { name: 'home', setup() { //经过ref定义响应式对象 const count = ref(0) //经过value属性访问值 console.log(count.value) //0 //经过return暴露出去,供template中使用 return { count } } }</script>复制代码
toRef 能够用来为一个 reactive 对象的属性建立一个 ref,这个 ref 能够被传递而且可以保持响应性(将 reactive 对象中的某个值转化为响应式数据)
与 ref 的区别:
<script>//引入须要的APIimport { reactive, toRef } from 'vue'export default { name: 'home', setup() { //经过reactive定义响应式对象 const obj = reactive({ count: 0, name: '廊坊吴彦祖' }) //经过toRef将obj中的count属性建立为ref const count = toRef(obj, 'count') //经过return暴露出去,供template中使用 return { obj, count } } } </script>复制代码
toRefs 把一个响应式对象转换成普通对象,该普通对象的每一个属性都是一个 ref ,和响应式对象属性一一对应(将 reactive 对象中的全部值转化为响应式数据)
<script>//引入须要的APIimport { reactive, toRefs } from 'vue'export default { name: 'home', setup() { //经过reactive定义响应式对象 const obj = reactive({ count: 0, name: '廊坊吴彦祖' }) //经过toRefs将obj中的全部属性建立为ref const refObj = toRefs(obj) console.log(refObj) //经过return暴露出去,供template中使用 return { obj, refObj } } } </script>复制代码
console.log(refObj):
readonly 传入一个对象(响应式或普通)或 ref,返回一个原始对象的只读代理(没法修改)
<template> <div> {{ count }} </div></template><script>//引入须要的APIimport { readonly } from 'vue'export default { name: 'home', setup() { //经过readonly定义只读对象 const count = readonly(0) count.value++ //修改报错!Cannot create property 'value' on number '0' //经过return暴露出去,供template中使用 return { count } } }</script>复制代码
computed(计算属性)传入一个 getter 函数,返回一个默认不可手动修改的 ref 对象,使用和 vue 2.0 区别不大
<template> <div> {{ count }} {{ addCount }} </div></template><script>//引入须要的APIimport { ref, computed } from 'vue'export default { name: 'home', setup() { //经过ref定义响应式对象 const count = ref(0) //经过computed方法实现计算属性 const addCount = computed(() => count.value + 1) console.log(addCount.value) //1 //经过return暴露出去,供template中使用 return { count, addCount } } }</script>复制代码
watch(侦听器)须要侦听特定的数据源,并在回调函数中执行反作用,使用和 vue 2.0 区别不大
<script>//引入须要的APIimport { ref, reactive, watch } from 'vue'export default { name: 'home', setup() { //经过ref定义响应式对象 const count = ref(0) //经过watch方法实现对count的数据侦听 watch(count, (newVal, oldVal) => { console.log('newVal:' + newVal) console.log('oldVal:' + oldVal) }) //经过reactive定义响应式对象 const obj = reactive({ count: 0 }) //经过watch方法实现对obj.count的数据侦听 watch(() => obj.count, (newVal, oldVal) => { console.log('newVal:' + newVal) console.log('oldVal:' + oldVal) }) //经过watch方法实现同时对count和obj.count的数据侦听 watch([count, () => obj.count], ([newVal1, oldVal1], [newVal2, oldVal2]) => { console.log('newVal1:' + newVal1) console.log('oldVal1:' + oldVal1) console.log('newVal2:' + newVal2) console.log('oldVal3:' + oldVal2) }) //经过return暴露出去,供template中使用 return { count, obj } } } </script>复制代码
watchEffect 当即执行传入的一个函数,并响应式追踪其依赖,并在其依赖变动时从新运行该函数
//watchEffect方法返回stop方法,执行该方法中止侦听const stop = watchEffect(() => { ...... }) //中止侦听stop()复制代码
与 watch 的区别:
<script>//引入须要的APIimport { ref, reactive, watchEffect } from 'vue'export default { name: 'home', setup() { //经过ref定义响应式对象 const count = ref(0) //经过reactive定义响应式对象 const obj = reactive({ count: 0 }) //经过watchEffect方法实现数据侦听(只能获得变化后的值) watchEffect(() => { console.log(count.value) console.log(obj.name) }) //经过return暴露出去,供template中使用 return { count, obj } } } </script>复制代码
使用组合式 API 时,reactive refs 和 template refs 的概念已是统一的。为了得到对模板内元素或组件实例的引用,须要在 setup() 中声明一个值为 null 的 ref 并返回它
<template> <div ref="box"></div></template><script>//引入须要的APIimport { ref, onMounted } from 'vue'export default { name: 'home', setup() { //经过ref(null)获取指定元素 const box = ref(null) onMounted(() => { console.log(box.value) //<div></div> }) //经过return暴露出去,供template中使用 return { box } } }</script>复制代码
vue3.0 生命周期部分有所变化(setup 函数代替了 beforeCreate 和 created 两个生命周期函数,在生命周期 beforeCreate 以前被调用)
vue 2.0 | vue3.0 |
---|---|
beforeCreate | setup |
created | setup |
beforeMount | onBeforeMount |
mounted | onMounted |
beforeUpdate | onBeforeUpdate |
updated | onUpdated |
beforeDestory | onBeforeUnmount |
destoryed | onUnmounted |
使用 vue3.0 的生命周期,一样须要先从 vue 中导入,再在 setup 函数中使用
<script>//引入生命周期import { onBeforeMount, onMounted, onBeforeUpdate, onUpdated, onBeforeUnmount, unMounted } from 'vue' export default { name: 'home', setup() { //组件挂载前 onBeforeMount(() => { ...... }) //组件挂载先后 onMounted(() => { ...... }) //组件更新前 onBeforeUpdate(() => { ...... }) //组件更新后 onUpdated(() => { ...... }) //组件销毁前 onBeforeUnmount(() => { ...... }) //组件销毁后 unMounted(() => { ...... }) return { ...... } } } </script>复制代码
npm install @vue/composition-api --save
import Vue from 'vue'import App from './App.vue'//引入@vue/composition-apiimport VueCompositionAPI from '@vue/composition-api'Vue.use(VueCompositionAPI) Vue.config.productionTip = falsenew Vue({ render: h => h(App), }).$mount('#app')复制代码
vue create vue3.0-demo
选择 Vue 3
vue3.0 下 vue-router4.0 相关 API:
//引入路由相关APIimport { createRouter, createWebHashHistory } from 'vue-router'import Home from '../views/home/index.vue'import About from '../views/about/index.vue'//建立hash路由模式const routerHashHistory = createWebHashHistory()//建立路由实例const router = createRouter({history: routerHashHistory,routes: [ {name: 'home',path: '/',component: Home }, {name: 'about',path: '/about',component: About } ] })export default router复制代码
import { createApp } from 'vue'import App from './App.vue'//引入路由实例import router from './router'//将路由实例注入到vue根实例中createApp(App).use(router).mount('#app')复制代码