Plugins 找不到Vuejs、插件、全局方法/属性

Plugins 找不到Vuejs、插件、全局方法/属性,plugins,vue.js,Plugins,Vue.js,我做错了什么?我安装了一个插件,并设置了一个全局属性,但在我的方法中,我无法访问该属性 my root Vue组件的main.js import Vue from 'vue' ... Vue.use({ install (Vue) { let googleAuth = null console.log('called!') Vue.auth = { setAuth (auth) { googleAuth = auth },

我做错了什么?我安装了一个插件,并设置了一个全局属性,但在我的方法中,我无法访问该属性

my root 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
      }
    }
  }
})
<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>
根vue组件中的App.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
      }
    }
  }
})
<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>
错误

未捕获的TypeError:无法读取未定义的属性“setAuth” 在VueComponent.onSignensAccess评估处,57 0.83049e6…热更新.js:13,:17:16 在boundFn eval处,在app.js:630,:126:14处 在H_。cb=gapi.loaded_0:285 在cb=gapi时,加载0:175 在h.r2时,cb=gapi.加载0:78 在xs cb=gapi.loaded_0:81时 在Wq cb=gapi.加载0:81时 在u.C.uea cb=gapi.loaded_0:80 在Ap cb=gapi.loaded_0:74时


正如@Srinivas Damam指出的,我需要使用prototype进行设置


Vue.prototype.auth修复了它

,正如@Srinivas Damam指出的,我需要使用prototype设置它

Vue.prototype.auth修复了它

我想你应该执行Vue.prototype.auth=someObj以便新创建的组件可以继承它们。我想你应该执行Vue.prototype.auth=someObj以便新创建的组件可以继承它们。