Vue3学习笔记(拾陆)

81 阅读1分钟

【6月日新计划更文活动】第28天

全局API的转移

  • Vue2 有许多全局 API 和配置

    • 例如:注册全局组件、注册全局指令等

      // 注册全局组件
      Vue.component('MyButton', {
        data: () => ({
          count: 0
        }),
        template: `<button @click="count++">Clicked {{ count }} times. </button>`
      })
      // 注册全局指令
      Vue.directive('focus', {
        inserted: el => el.focus()
      })
      
  • Vue3中对这些API做出了调整

    • 将全局的 API,即:Vue.xxx调整到应用实例 (app) 上
    -- 2.x全局 API (Vue) ---- 3.x实例 API (app) --
    Vue.config.xxxapp.config.xxx
    Vue.config.productionTip移除 (生产版本提示)
    Vue.componentapp.component
    Vue.directiveapp.directive
    Vue.mixinapp.mixin
    Vue.useapp.use
    Vue.prototypeapp.config.globalProperties (全局属性)