상위 구성 요소의 한 Vue 구성 요소에서 저장된 vanilla 클래스 인스턴스에 액세스하는 방법
둘 다 동일한 기본 구성 요소에서 상속되는 두 개의 하위 구성 요소를 가진 상위 구성 요소가 있습니다(이 상위 구성 요소는 Vue Storybook에서 생성되어 사용되고 있습니다).둘다요.SiblingAComponent
그리고.SiblingBComponent
같은 것을 계승하다BaseComponent
및 동일한 상속된 데이터를 인스턴스화합니다.classInstance
이것은 다른 라이브러리의 바닐라 JS 클래스 인스턴스입니다.접속을 시도하고 있습니다.classInstance
상위 컴포넌트에서 두 번째 형제 컴포넌트로 데이터 전달(이 경우 에서)SiblingAComponent
로.SiblingBComponent
)를 참조해 주세요.siblingARef
그러나 스토리북 컴파일러에서 다음과 같은 오류가 발생합니다.
too much recursion
isArguments@http://localhost:6006/vendors~main.9107ef8d0bc0558399e1.bundle.js:49010:16
keys@http://localhost:6006/vendors~main.9107ef8d0bc0558399e1.bundle.js:49073:28
_traverse@http://localhost:6006/vendors~main.9107ef8d0bc0558399e1.bundle.js:119972:19
_traverse@http://localhost:6006/vendors~main.9107ef8d0bc0558399e1.bundle.js:119974:28
Parent Component 스토리:
storiesOf("ParentComponent Story", module)
.addDecorator(
withKnobs({
escapeHTML: false
})
)
.add("Passing data from A to B", () => ({
name: 'ParentComponent',
components: {
SiblingAComponent,
SiblingBComponent,
},
data() {
return {
siblingAData: [....], // array of objects
siblingAOptions: {
axes: {},
height: "50px",
},
siblingBData: [...], // array of objects
siblingBOptions: null,
}
},
mounted() {
const siblingAInstance = this.$refs.siblingARef.classInstance;
const newOptions = {
legend: {
external: {
reference: siblingAInstance,
},
},
};
// this line is where I am getting an error
this.siblingBOptions = legendExternal;
},
template: `
<SiblingAComponent ref="siblingARef" :data="siblingAData" :options="siblingAOptions"/>
<SiblingBComponent v-if="siblingBData" :data="siblingBData" :options="siblingBOptions"/>
`,
}));
형제 AC 컴포넌트:
<template>
<div class="sibling-a-component"></div>
</template>
<script>
import { ComponentA } from '@libraryexample/components';
import BaseComponent from './base-component.vue';
export default {
name: 'SiblingAComponent',
extends: BaseComponent,
mounted() {
this.classInstance = new ComponentA(this.$el, {
data: this.data,
options: this.options,
});
},
};
</script>
형제 BC 구성 요소:
<template>
<div class="sibling-b-component"></div>
</template>
<script>
import { ComponentB } from '@libraryexample/components';
import BaseComponent from './base-component.vue';
export default {
name: 'SiblingBComponent',
extends: BaseComponent,
mounted() {
this.classInstance = new ComponentB(this.$el, {
data: this.data,
options: this.options,
});
},
};
</script>
기본 컴포넌트:
<script>
export default {
name: 'BaseComponent',
data() {
return {
classInstance: null,
};
},
props: {
data: { type: [Object, Array], required: true },
options: { type: Object, required: true },
},
};
</script>
Angular 및 React의 세계에서는 다른 컴포넌트의 Vanilla 클래스 인스턴스에 액세스하기 위한 참조를 사용하는 것은 새로운 것이 아닙니다.Vue는 처음이라 클래스 인스턴스에 액세스하려고 하면 (원시적인 데이터 타입은 정상적으로 동작) 실패하여 이렇게 이상한 오류가 발생하는 이유는 무엇입니까?재귀는 어디에서 발생합니까?
언급URL : https://stackoverflow.com/questions/62635406/how-to-access-vanilla-class-instance-stored-from-one-vue-component-in-a-parent-c
'programing' 카테고리의 다른 글
int 번호의 개별 숫자를 얻는 방법은 무엇입니까? (0) | 2022.06.29 |
---|---|
Jsx를 사용하는 경우 Vuejs 2.x에서 fragment를 사용하는 방법 (0) | 2022.06.29 |
int의 사이즈는 올바른데 int의 사이즈는 잘못된 이유는 무엇입니까? (0) | 2022.06.29 |
Vuetify 아이콘이 올바르게 표시되지 않음: "$vuetify.icons"로 표시됩니다.." (0) | 2022.06.28 |
다른 소품 검증기에서 소품 값에 액세스 (0) | 2022.06.28 |