方法一: 使用Vue.prototype
Vue.prototype.myCode = function() {
...
}
this.myCode()
方法二: 使用exports.install + Vue.prototype
exports.install = function (Vue) {
Vue.prototype.myCode= function (){
...
}
};
import myCode from './myCode'
Vue.use(myCode)
this.myCode()
- 在使用
exports.install
方法时, 运行报错exports is not defined
解决方法
export default {
install(Vue) {
Vue.prototype.myCode = {
...
}
}
}
方法三:使用全局变量模块文件
<script>
const token='12345678';
export default {
methods: {
getToken(){
....
}
}
}
</script>
- 在需要的地方引用进全局变量模块文件,然后通过文件里面的变量名字获取全局变量参数值。
<script>
import global from '../../components/Global'
export default {
data () {
return {
token:global.token
}
},
created: function() {
global.getToken();
}
}
</script>