【发布时间】:2017-10-27 23:06:53
【问题描述】:
我正在创建一个插件,我只是想知道为什么我无法在main.js 文件中访问它。以下是Auth.js 的样子:
const Auth = {
install(Vue) {
Vue.prototype.$isGuest = function () {
console.log('This user is a guest.');
}
Vue.prototype.$getAuthToken = function () {
console.log('Auth token will be returned.');
}
}
}
export default Auth
这是 main.js:
import Auth from '@/helper/Auth'
Vue.use(Auth)
但是,当我执行console.log(this.$isGuest()) 时,它不起作用。它实际上返回以下内容:
main.js?1c90:25 Uncaught TypeError: this.$isGuest is not a function
问题是,当我在 Dashboard.vue 之类的组件中调用它时,此方法有效。
我有办法避免在main.js 中调用isGuest 方法(我可以在Layout.vue 中调用它),但我更好奇为什么它在main.js 中不起作用。
可能是因为Vue还没有初始化,但是即使我把console.log()放在文件末尾,还是不行。
谢谢, N.
【问题讨论】:
-
main.js 中的
console.log在哪里? -
@Bert 低于
Vue.use(Auth)高于new Vue({...。