2017-12-17 2 views
0

저는 Vuejs 2에서 새롭습니다. 나는 phonegap을 사용하여 응용 프로그램을 만들려고합니다. 최근에 저는 vue2-touch-event를 사용 중이며 일부 터치 이벤트를 처리하려고합니다. 사용자가 왼쪽/오른쪽으로 스 와이프하면 이벤트와 함께 추가 매개 변수가 전달됩니다. 여기vuejs2-touch-event에서 정의되지 않은 오류

내가 매개 변수

<label v-touch:swipe.right="doneLisItem(index)"> 
</label> 

다음 스크립트

data() { 
    return { 
     newList: '', 
     todoLists: [], 
    }; 
}, 
methods: { 
    doneLisItem(index) { 
     return function(direction, event) { 
      console.log(index); 
      console.log(this.todoLists); 
      if (!this.todoLists[index].completed) { 
       this.todoLists[index].completed = true; 
      } 
     }; 
    }, 
} 

문제는 내가 CONSOLE.LOG (this.todoLists)에 정의되지 않은 받고있어입니다 내 코드가 통과하는 방법입니다. 아무도 나를이 문제를 해결하는 데 도움이 될 수 있습니다. TIA

+0

아마도 리턴 기능을 사용하지 마십시오. 정상적인 함수를 사용하고 동일한 로직을 사용하십시오. – samayo

+0

@samayo vuejs2-touch-event 핸들러 함수에 매개 변수를 전달하면 return 함수를 사용해야합니다. 어느 쪽이든 이벤트를 듣지 않을 것이다. –

답변

0

반환 된 콜백이 호출 될 때 this은 Vue 인스턴스를 가리 키지 않습니다. 이것이 데이터 등록 정보에 액세스 할 수없는 이유이며 undefined이 콘솔에 기록됩니다.

은 그래서 this는 VUE 예를

doneLisItem(index) { 
    return (direction, event) => { 
     console.log(index); 
     console.log(this.todoLists); 
     if (!this.todoLists[index].completed) { 
      this.todoLists[index].completed = true; 
     } 
    }; 
}, 

으로 어휘 및 포인트 바인딩 또는 함수

내부의 정확한 VUE 인스턴스 변수 향해 변수를 가리키는 선언하여 closure의 사용을 만들 수 그래서 arrow function를 반환
methods: { 
    doneLisItem(index) { 
     var vm = this; //now the Return function has a closure over vm which is the correct vue instance 
     return function(direction, event) { 
      console.log(index); 
      console.log(vm.todoLists); 
      if (vm.todoLists[index].completed) { 
       vm.todoLists[index].completed = true; 
      } 
     }; 
    }, 
} 
+0

. 나는 이것을 그리워 했어 ... 매력처럼 작동한다. –

+0

감사합니다. @Vamsi Krishna .. 전에 이런 식으로 시도했는데, 다른 오류로 작동하지 않았을 수도 있습니다. 당신의 응답을 주셔서 감사합니다. –