通过vue.use来加载自定义的vue插件

时间:2019-09-08 11:23:16 类型:vue
字号:    

1. 在components下定义pub.js  

//方法1 
exports.install = function (Vue, options) {
      Vue.prototype.baseUrl = 'http://www.ncyteng.com/';
      Vue.prototype.getMyname = function(){
                console.log("南昌雅腾")
      }
 };
//方法2
export default{
    install(Vue,options){
        Vue.prototype.baseUrl = 'http://www.ncyteng.com/';
        Vue.prototype.getMyname = function(){
                  console.log("南昌雅腾")
        }
    }
}

2. 在main.js/main.ts文件中引用 


import pub from './components/pub';
  Vue.use(pub);
3. 在.vue文件中调用
 console.log(this.baseUrl);
 this.getMyname();