반응형
생성된 이벤트의 워치 속성과 $watch 메서드의 차이(Vue)
Vue에서는 워치 속성과 $watch 방식의 차이가 있습니까?예를 들어, 작성자가 아래 코드의 watch 속성 대신 $watch 이벤트를 사용하는 이유를 이해하려고 합니다.
created () {
const routes = this.mainMenu.find(item => item.path === '/')
this.menus = (routes && routes.children) || []
// 处理侧栏收起状态
this.$watch('collapsed', () => {
this.$store.commit(SIDEBAR_TYPE, this.collapsed)
})
this.$watch('isMobile', () => {
this.$store.commit(TOGGLE_MOBILE_TYPE, this.isMobile)
})
},
$watch를 아래와 같이 옮기면 차이가 있나요?
watch: {
isMobile : function(value) {
this.$store.commit(TOGGLE_MOBILE_TYPE, value)
},
collapsed : function(value) {
this.$store.commit(SIDEBAR_TYPE, value)
}
},
created () {
}
조언 부탁드립니다.
그watch
옵션 사용this.$watch
보닛 밑의 수법, 그러니까 기본적으로 똑같아요.
유일한 차이점은 이다.this.$watch
는 워처를 정지하기 위해 호출할 수 있는 함수를 반환합니다.
const queryWatcher = this.$watch('$route.query', doSomethingFunction)
...
queryWatcher() // Stop the watcher
사용해도 소용없다this.$watch
정지 기능이 필요 없기 때문에 안전하게 이동시킬 수 있습니다.watch
옵션 속성.
언급URL : https://stackoverflow.com/questions/68176042/difference-in-watch-properties-vs-watch-method-in-created-event-vue
반응형
'programing' 카테고리의 다른 글
접속 거부 오류의 원인은 무엇입니까? (0) | 2022.08.14 |
---|---|
const 변수를 사용하여 C에서 배열 크기를 선언할 수 있습니까? (0) | 2022.08.14 |
fork()에 의해 작성된 프로세스 간에 메모리를 공유하려면 어떻게 해야 합니까? (0) | 2022.08.14 |
v-expansion-panel-content에 대한 동적 콘텐츠 (0) | 2022.08.14 |
.equals()를 생성할 때 인스턴스보다 getClass()를 선호하는 이유가 있습니까? (0) | 2022.08.14 |