欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

vue 全局函数

程序员文章站 2022-06-22 17:09:03
...

1. common.js 公用方法

export default {
  install (Vue, options) {
    Vue.prototype.fetchI18nText = function (locale) {
      this.$http.get('http://localhost:8081/static/i18n/' + locale + '.json').then((response) => {
        Vue.prototype.$i18nText = response.data
})
    }
  }
}

2. main.js 引用

import common from './common.js'
Vue.use(common)

3. any.vue 调用方法

export default {
<script>
  export default {
    name: 'hello',methods: {changeLocale () {
        this.fetchI18nText('zh_CN')
      }
    }
  }
}