vue2 3对比
全局API
vue 2
- vue2 注册全局组件
vue.component
Vue.component('btn-cnt',{
data:()=>({
cnt:0
}),
template: '<button @click="cnt++">clicked {{cnt}} times </button>'
})
Vue.directive('focus', {
inserted: el => el.focus()
})
- ==问题==
- vue2 通过
new Vue()
创建根Vue实例
- 同一个Vue构造函数创建的每个根实例共享相同的全局配置,很容易受到测试用例污染
- 同一个页面 多个app共享一个vue副本 变得困难
vue3
- 一个新的全局API ==createAPP== 返回应用实例
import { createApp } from 'vue'
const app = createApp({})
- Vue.use ====> app.use
- 挂载根组件实例 app.mount('#app')
const app = createApp(MyApp)
app.component('button-counter', {
data: () => ({
count: 0
}),
template: '<button @click="count++">Clicked {{ count }} times.</button>'
})
app.directive('focus', {
mounted: el => el.focus()
})
app.mount('#app')
组合式API
- 实际使用组合API位置 为setup 在创建组件之前执行