我在這做錯了什麼?我安裝了一個插件,並設置了全局屬性,但是在我的方法中,我無法訪問該屬性。Vuejs,Plugin,global method/property not found
main.js我的根Vue的成分
從根本Vue的組件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
}
}
}
})
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)
我猜你應該做'Vue.prototype.auth = someObj'使新創建的組件可以繼承他們。 –