簡體   English   中英

如何在Vue.js中訪問對象內部的方法?

[英]How to access methods inside object in vuejs?

我正在嘗試通過遵循Google文檔在Vue中使用Google登錄,並且一切正常,但是我無法訪問attachClickHandler內的方法。

new Vue({
    el: '#loginModal',
    data: { ...
    },
    methods: {
        gInit: function() {
            gapi.load('auth2', function() {
                auth2 = gapi.auth2.init({
                    client_id: 'MY-Client-id.apps.googleusercontent.com',
                    cookiepolicy: 'single_host_origin',
                    //scope: 'additional_scope'
                });
                auth2.attachClickHandler(document.getElementById('googleBtn'), {},
                    function(googleUser) {
                        const profile = googleUser.getBasicProfile();
                        const gplusObj = {
                            name: profile.getName(),
                            email: profile.getEmail(),
                            provider: 'google',
                            image: profile.getImageUrl(),
                            provider_user_id: profile.getId()
                        };

                        this.socialLogin(gplusObj);
                    },
                    function(error) {
                        alert(JSON.stringify(error, undefined, 2));
                    });
            });
        },
        socialLogin: function(data) {
            axios.post(`${this.api}/api/sociallogin`, data)
                .then(res => {
                    console.log(res);
                }).catch(err => {
                    console.log(err);
                });
        },
    },
    mounted: function() {
        this.gInit();
    }

})

這里調用一個函數socialLogin()attachClickHandler()是給錯誤this.socialLogin is not a function沒有定義。 為什么這不起作用?

這是因為this.socialLogin調用位於回調函數中。 此函數創建了一個新的上下文,因此this發生變化,不再是您的組件。 使用箭頭功能。 他們不會改變this

編輯:像這樣更改您的代碼:

gInit() {
    gapi.load('auth2', () => {
        ...
        auth2.attachClickHandler(document.getElementById('googleBtn'), {}, (googleUser) => {
            ...
            this.socialLogin(gplusObj);
        }, (error) => {
            alert(JSON.stringify(error, undefined, 2));
        });
    });
},

關於該主題的更多信息: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions ,請參閱“對此不做任何區分”。

由於您沒有保存this ,請嘗試:

 new Vue({ el: '#loginModal', data: { ... }, methods: { gInit: function() { let self = this // new added gapi.load('auth2', function() { auth2 = gapi.auth2.init({ client_id: 'MY-Client-id.apps.googleusercontent.com', cookiepolicy: 'single_host_origin', //scope: 'additional_scope' }); auth2.attachClickHandler(document.getElementById('googleBtn'), {}, function(googleUser) { const profile = googleUser.getBasicProfile(); const gplusObj = { name: profile.getName(), email: profile.getEmail(), provider: 'google', image: profile.getImageUrl(), provider_user_id: profile.getId() }; console.log(e); self.socialLogin(gplusObj); //updated }, function(error) { alert(JSON.stringify(error, undefined, 2)); }); }); }, socialLogin: function(data) { axios.post(`${this.api}/api/sociallogin`, data) .then(res => { console.log(res); }).catch(err => { console.log(err); }); }, }, mounted: function() { this.gInit(); } }) 

this不會自動通過,在學習vue之前您應該已經知道這一點。

另外,您可以使用arrow function來避免this更改:

 new Vue({ el: '#loginModal', data: { ... }, methods: { gInit: function() { gapi.load('auth2', function() { auth2 = gapi.auth2.init({ client_id: 'MY-Client-id.apps.googleusercontent.com', cookiepolicy: 'single_host_origin', //scope: 'additional_scope' }); auth2.attachClickHandler(document.getElementById('googleBtn'), {}, (googleUser) => { //updated const profile = googleUser.getBasicProfile(); const gplusObj = { name: profile.getName(), email: profile.getEmail(), provider: 'google', image: profile.getImageUrl(), provider_user_id: profile.getId() }; console.log(e); this.socialLogin(gplusObj); }, function(error) { alert(JSON.stringify(error, undefined, 2)); }); }); }, socialLogin: function(data) { axios.post(`${this.api}/api/sociallogin`, data) .then(res => { console.log(res); }).catch(err => { console.log(err); }); }, }, mounted: function() { this.gInit(); } }) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM