그래서 나는 Vue 컴포넌트에 props를 전달하고 싶지만, 이러한 props는 예를 들어 AJAX를 사용하여 내부에서 Vue 컴포넌트를 업데이트 할 때 그 컴포넌트 내부에서 미래에 변경 될 것으로 예상합니다. 따라서 구성 요소 초기화에만 사용됩니다.
cars-list초기 속성이있는 소품을 다음 위치에 전달하는 내 Vue 구성 요소 요소 single-car:
// cars-list.vue
<script>
export default {
data: function() {
return {
cars: [
{
color: 'red',
maxSpeed: 200,
},
{
color: 'blue',
maxSpeed: 195,
},
]
}
},
}
</script>
<template>
<div>
<template v-for="car in cars">
<single-car :initial-properties="car"></single-car>
</template>
</div>
</template>
내가 지금 수행하는 방식은 내 single-car구성 요소 this.initialProperties내 this.data.properties에서 created()초기화 후크 에 할당 하는 것 입니다 . 그리고 그것은 작동하고 반응합니다.
// single-car.vue
<script>
export default {
data: function() {
return {
properties: {},
}
},
created: function(){
this.data.properties = this.initialProperties;
},
}
</script>
<template>
<div>Car is in {{properties.color}} and has a max speed of {{properties.maxSpeed}}</div>
</template>
하지만 내 문제는 그것이 올바른 방법인지 모르겠다는 것입니다. 그것은 나에게 길을 따라 약간의 문제를 일으키지 않습니까? 아니면 더 좋은 방법이 있습니까?
data되어two-way구속,하지만 당신은 통과 할 수없는data구성 요소, 당신은 통과props,하지만 당신은 수신 한 변경할 수props없으며 변환props에data. 그리고 뭐? 제가 배운 한 가지는 전달props하고 이벤트를 트리거 해야한다는 것입니다. 즉, 컴포넌트가props수신 한 내용 을 변경하려면 이벤트를 호출하고 "렌더링"해야합니다. 그러나 당신은one-wayReact와 똑같은 바인딩을 남겼고 나는data그때에 대한 사용법을 보지 못했습니다 . 꽤 혼란 스럽습니다.