ichenchao

原文:

两种情况:

1. js为ES5的写法时,如下(自定义的my.js):

1
2
3
function fun(){
    console.log(\'hello\');     
}

 Vue中的全局引入方式为,在index.html中通过如下方式引入即可:

1
<script src="src/models/my.js"></script>

2. js为 ES6 模块化写法时,即 import,export形式,如下:

1
2
3
4
var fun=function(){
    console.log(\'hello\');
}
export default fun;

 Vue中全局引入的方式为,在main.js中添加如下代码:

1
2
import fun from \'src/models/my.js\';
Vue.prototype.$xx=fun;  //其中$xx为新命的名。

 使用方法为,在要调用的地方使用如下代码调用:

1
var aa=this.$xx;

  注意,模块化引入方式时,要引入的 js export的值只可为一个,若多余一个如 export {var1,var2,...} 则不可使用这种方式 (经验证无效)。

分类:

技术点:

相关文章: