nuxt.js全局方法的使用

1.在plugins的文件夹地下去建立一个proto.js的文件

import Vue from 'vue'
var test = {
    install(Vue) {
        Vue.prototype.test = {
            val: function(val) {
                console.log(9999);
            }
        };
        Vue.prototype.testname = '周欢'
    }
}
Vue.use(test)

2.直接使用

 created() {
    console.log(this.$route.params, 'this.$route');  
    console.log(this.testname, 'this'); //全局去拿字符串
  },
  mounted() {
    this.test.val()  //全局去调用事件
  }

在created里面去调用事件会报错

原文地址:https://www.cnblogs.com/antyhouse/p/12553073.html