簡體   English   中英

如何在VueJS上使用數據函數的變量?

[英]How to use variables of data function on VueJS?

我對VueJS有一個愚蠢的問題。 我是VueJS的新手。 我想訪問和更改數據功能的變量。 但是我做不到。

出現錯誤的行:

console.log('item: ' + this.item);

錯誤在這里:

TypeError:無法讀取未定義的屬性“ item”

這是我的代碼:

data: function(){
    return {
        item: 69,
        file: 0
    };
},

methods: {
    toggle: (elementId = 0, type = 'item') => {
        console.log('element ID: ' + elementId);
        console.log('type: ' + type);
        console.log('item: ' + this.item);

        switch (type) {
            case 'item':
                break;
            case 'file':
                break;
        }
    }
}

使用toggle(elementId = 0, type = 'item') {}而不是toggle: (elementId = 0, type = 'item') => {}
箭頭功能分配this到其父的this范圍。
請參閱: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Arrow_functions_used_as_methods

暫無
暫無

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

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