2017-04-01 8 views
1

私はここで間違っていますか?私はプラグインをインストールし、グローバルプロパティを設定しますが、私のメソッドではそのプロパティにアクセスできません。私のルートVueのコンポーネントルートVueのコンポーネントからVuejs、プラグイン、グローバルメソッド/プロパティが見つかりません

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ため

main.js

<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> 

@Srinivas Damamが指摘したように私は必要な

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

それを修正しました。 –

答えて

3

エラープロトタイプを使用して設定します。

Vue.prototype.authは、私が新たに作成されたコンポーネントは、それらを継承することができるようにあなたが `Vue.prototype.auth = someObj`を行う必要がありますね

関連する問題