2017-04-01 71 views
1

我在这做错了什么?我安装了一个插件,并设置了全局属性,但是在我的方法中,我无法访问该属性。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)

+4

我猜你应该做'Vue.prototype.auth = someObj'使新创建的组件可以继承他们。 –

回答

3

由于@Srinivas Damam指出我需要使用原型进行设置。

Vue.prototype.auth固定它