鍍金池/ 問答/HTML/ vue.js 無法讀取data 數(shù)據(jù)里面的item

vue.js 無法讀取data 數(shù)據(jù)里面的item

Uncaught TypeError: Cannot read property 'items' of undefined

不論用this.items還是 items 都無法讀取,并且奇怪的是type==1 里面可以用this.items 讀取到data里面的items 但是type==2里面就不行了 不知道為啥 求教我
<button type="button" id="allNotes" v-on:click="showNote(1)">

             <span>allNotes</span>
         </button>
         <button id="Favorites" v-on:click="showNote(2)">
             <span>Favorites</span>
         </button>
         
         
         

export default {

    data:function(){
        return{
            items:this.$parent.Notes,
        }
    },
    methods:{
        showNote:function(type){
            this.items='';
            if(type==1){
                this.items=this.$parent.Notes;
            }
            if(type==2){
                this.$parent.Notes.forEach(function(c){
                    if(c['favorite']){
                        console.log(this.items);
                        
                    }
                });
            }
        }
回答
編輯回答
淚染裳

圖片描述
在type == 2 那個里 你調(diào)用了 循環(huán),在循環(huán)里this不是指的vue實例
所以才會報錯

2018年1月27日 21:53
編輯回答
荒城

你可以試一下 else if

2018年1月5日 16:34
編輯回答
朕略傻

是不是this的指向問題?

2017年10月5日 23:51
編輯回答
膽怯
if(type==2){
            let that = this;
            this.$parent.Notes.forEach(function(c){
                if(c['favorite']){
                    console.log(that.items);
                    
                }
            });
  }
  或者用箭頭 
2017年1月4日 00:59
編輯回答
青裙
this.$parent.Notes.forEach(c => {
                    if(c['favorite']){
                        console.log(this.items);
                        
                    }
                });
2018年5月1日 15:09