1、新建插件文件Plugins.js:
1 export default {
2 install(Vue) {
3 Vue.prototype.$const = { //常量
4 URL: "http://www.fly63.com/", //项目请求接口的url
5 }
6 Vue.prototype.$utils = { //全局方法
7 getUrlParam: function (name) { //获取url中的参数
8 var reg = new RegExp('(^|&?)' + name + '=([^&]*)(&|$)', 'i');
9 var r = window.location.href.substr(1).match(reg);
10 if (r != null) {
11 return decodeURI(r[2]);
12 }
13 return undefined;
14 }
15 }
16 }
17 }
2、main.js入口函数,配置如下:
1 import Plugins from './assets/js/Plugins.js'// 引入
2
3 Vue.use(Plugins) //通过全局方法 Vue.use() 使用插件
3、在组件中的使用:
1 created: function () {
2 console.log(this.$const.URL); // 调用常量
3 var name = this.$utils.getUrlParam('name'); // 调用方法
4 }