vue中引入自定义的js文件

1.创建utils/index.js文件

其结构如下:
import { Toast } from ‘mint-ui’;
function Toasts (text){ //mint-ui
Toast({
message: text,
position: ‘middle’,
className:‘toastclass’,
duration: 2000
});
}
function urllist(){
var url = “http://www.diniver.com/index/”;
return url;
}
function urllist2(){
var url = “http://www.diniver.com/index/”;
return url;
}

export { //返回函数模块
Toasts,
urllist,
urllist2,
}

2. main.js中引用

import {urllist, Toasts} from ‘./utils/index.js’ //导入自定义函数
Vue.prototype. u r l l i s t = u r l l i s t / / 把 自 定 义 函 数 挂 载 到 v u e 原 型 V u e . p r o t o t y p e . urllist = urllist //把自定义函数挂载到vue原型 Vue.prototype. urllist=urllist//vueVue.prototype.toasts = Toasts

3. 子组件中调用

this.$urllist()

你可能感兴趣的:(vue,引用自定义js)