我在这里做错了什么?我安装了一个插件,并设置了一个全局属性,但是在我的方法中我无法访问该属性。
我的根 Vue 组件的 main.js
import Vue from 'vue'
...
Vue.use({
install (Vue) {
let googleAuth = null
console.log('called!')
Vue.auth = {
setAuth (auth) {
googleAuth = auth
},
isAuthenticated () {
return googleAuth !== null && googleAuth.isSignedIn.get()
},
currentUser () {
return googleAuth.currentUser.get()
},
currentUserProfile () {
return this.currentUser().getBasicProfile()
},
getIdToken () {
return this.currentUser().getAuthResponse().id_token
}
}
}
})
来自根 Vue 组件的 App.vue
<template></template>
<script>
export default {
name: 'Trudit',
data () {
return {
title: 'Trudit'
}
},
methods: {
onSignInSuccess (googleUser) {
console.log(this)
this.auth.setAuth(googleUser) //<- this.auth === undefined
}
}
}
</script>
错误
Uncaught TypeError: Cannot read property 'setAuth' of undefined at VueComponent.onSignInSuccess (eval at 57 (0.83049e6….hot-update.js:13), :17:16) at boundFn (eval at (app.js:630), :126:14) at H_. (cb=gapi.loaded_0:285) at cb=gapi.loaded_0:175 at h.r2 (cb=gapi.loaded_0:78) at xs (cb=gapi.loaded_0:81) at Wq (cb=gapi.loaded_0:81) at _.C.uea (cb=gapi.loaded_0:80) at Ap (cb=gapi.loaded_0:74)
最佳答案
正如@Srinivas Damam 所指出的,我需要使用原型(prototype)来设置它。
Vue.prototype.auth
修复了它
关于plugins - Vuejs,插件,找不到全局方法/属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43156014/