programing

Vue 변환 변경 인스턴스(MyArray에서 어레이로)

prostudy 2022. 7. 27. 21:34
반응형

Vue 변환 변경 인스턴스(MyArray에서 어레이로)

빌트 확장 중MyArray 클래스가 포함된 어레이에서 MyArr을 Vuex에 저장하려고 하면 인스턴스가 손실되어 bultIn Array를 가져옵니다.

뭐가 잘못됐나요?

class MyArray extends Array{/*some methods*/}

const state = {
  arr: new ModuleOperationArray(),
}

const mutations = {
  setArr: (state) => {
   console.log(state.arr instanceof MyArray); //false -- WHY?

   let arr = new MyArray();
   console.log(arr instanceof MyArray); //true -- expectedly

   state.arr = arr;
   console.log(state.arr instanceof MyArray); //false -- WHY?
  }
}

어레이를 비활성화하려면 Vue는 다음과 같은 다양한 방법을 패치해야 합니다.push그리고.pop독자적인 버전을 사용하고 있습니다.반응성 시스템을 트리거하면서 원본과 동일하게 작동합니다.

이 패치를 신속하게 적용하기 위해 Vue는 어레이의 프로토타입을 자체 확장 버전으로 전환합니다.참조:

https://github.com/vuejs/vue/blob/9fbd416635eb3d7b32cd73b7c29f8377003c4dc8/src/core/observer/index.js#L49

자세한 내용은 브라우저 지원에 따라 달라집니다.

instanceof운영자는 프로토타입 체인을 기반으로 값을 결정합니다.Vue가 스위치를 설정하면 어레이는 더 이상 사용할 수 없게 됩니다.MyArray시제품 체인으로요.

언급URL : https://stackoverflow.com/questions/59298285/vue-mutation-change-instance-myarray-to-array

반응형