vue学习笔记——注册组件

注册一个组件

// define a component first
const i18nPlugin = {
    install(Vue, locales){.  // 当执行Vue.use()的时候,会在全局执行这个函数
        Vue.prototype.$t = function(id){ // 在vue跟组件上注册$t函数,以后就可以直接通过$t进行使用了
            return locals[this.$root.lang][id] // this.$root在该组件中指向了根组件实例
        }
  }
}

Vue.use(i18n, /* option */ {
  en: { 'welcome-message': 'hello' },
  zh: { 'welcome-message': '你好' },
  nl: { 'welcome-message': 'Hallo' }
})

new Vue({
  el: '#app',
  data: {
    lang: 'en'
  },
  methods: {
    changeLang (lang) {
      this.lang = lang
    }
  }
})
原文地址:https://www.cnblogs.com/lyzz1314/p/15770237.html