兩種情況:
1. js為ES5的寫法時,如下(自定義的my.js):
function fun(){
console.log('hello');
}
Vue中的全局引入方式為,在index.html中通過如下方式引入即可:
<script src="src/models/my.js"></script>
2. js為 ES6 模塊化寫法時,即 import,export形式,如下:
var fun=function(){
console.log('hello');
}
export default fun;
Vue中全局引入的方式為,在main.js中添加如下代碼:
import fun from 'src/models/my.js'; Vue.prototype.$xx=fun; //其中$xx為新命的名。
使用方法為,在要調用的地方使用如下代碼調用:
var aa=this.$xx;
注意,模塊化引入方式時,要引入的 js export的值只可為一個,若多余一個如 export {var1,var2,...} 則不可使用這種方式 (經驗證無效)。
