我的應用程式作業正常,但出現錯誤的問題是,當我單擊任何選單時,出現以下錯誤,請幫助。干得好。
[Vue 警告]:避免直接改變 prop,因為每當父組件重新渲染時,值都會被覆寫。相反,根據道具的值使用資料或計算屬性。道具被變異:“selectedPost”
TabloStart.vue
<template>
<section >
<div >
<div >
<TableMenu :posts="posts" :selectedPost="selectedPost"></TableMenu>
</div>
</div>
</section>
</template>
<script>
export default {
name: "TabloStart",
data() {
return {
posts: [
{
id: 1,
title: "Cat Ipsum",
content:
"<p>Dont wait for the storm to pass, dance in the rain kick up litter decide to want nothing to do with my owner today demand to be let outside at once, and expect owner to wait for me as i think about it cat cat moo moo lick ears lick paws so make meme, make cute face but lick the other cats. Kitty poochy chase imaginary bugs, but stand in front of the computer screen. Sweet beast cat dog hate mouse eat string barf pillow no baths hate everything stare at guinea pigs. My left donut is missing, as is my right loved it, hated it, loved it, hated it scoot butt on the rug cat not kitten around</p>"
},
{
id: 2,
title: "Hipster Ipsum",
content:
"<p>Bushwick blue bottle scenester helvetica ugh, meh four loko. Put a bird on it lumbersexual franzen shabby chic, street art knausgaard trust fund shaman scenester live-edge mixtape taxidermy viral yuccie succulents. Keytar poke bicycle rights, crucifix street art neutra air plant PBR&B hoodie plaid venmo. Tilde swag art party fanny pack vinyl letterpress venmo jean shorts offal mumblecore. Vice blog gentrify mlkshk tattooed occupy snackwave, hoodie craft beer next level migas 8-bit chartreuse. Trust fund food truck drinking vinegar gochujang.</p>"
},
{
id: 3,
title: "Cupcake Ipsum",
content:
"<p>Icing dessert soufflé lollipop chocolate bar sweet tart cake chupa chups. Soufflé marzipan jelly beans croissant toffee marzipan cupcake icing fruitcake. Muffin cake pudding soufflé wafer jelly bear claw sesame snaps marshmallow. Marzipan soufflé croissant lemon drops gingerbread sugar plum lemon drops apple pie gummies. Sweet roll donut oat cake toffee cake. Liquorice candy macaroon toffee cookie marzipan.</p>"
}
],
selectedPost: null
}
}
}
</script>
表格選單.vue
<template>
<div >
<ul >
<li v-for="post in posts"
v-bind:key="post.id"
v-on:click="selectedPost = post"
>
<a href="#" v-bind: >
{{ post.title }}
</a>
</li>
</ul>
<div >
<div v-if="selectedPost"
>
<h3>{{ selectedPost.title }}</h3>
<div v-html="selectedPost.content"></div>
</div>
<strong v-else>
Click on a blog title to the left to view it.
</strong>
</div>
</div>
</template>
<script>
export default {
name: "TableMenu",
data() {
return {}
},
props: {
posts: [],
selectedPost: null,
}
}
</script>
uj5u.com熱心網友回復:
v-on:click="selectedPost = post"是罪魁禍首;selectedPost是這里的道具,您不能分配給道具。
根據您的需要,有兩種不同的解決方案:
- 制作
selectedPost本地資料屬性而不是道具。然后你可以修改,selectedPost但因為它不再是一個道具,你不能再selectedPost從父母那里接受(但你并沒有真正這樣做)。
data() {
return {
selectedPost: null
}
}
selectedPost您應該發出一個事件,而不是直接修改,父級可以處理該事件以更新其資料。約定是發出一個名為 like 的事件update:selectedPost,這將使其與.sync修飾符一起作業。
將點擊處理程式更改為:
@click="$emit('update:selectedPost', post)"
然后在父級中,更新如下:
<TableMenu
:posts="posts"
:selectedPost="selectedPost"
@update:selectedPost="selectedPost = $event"
></TableMenu>
或者您可以使用.sync使上述更簡單(它做同樣的事情):
<TableMenu
:posts="posts"
:selectedPost.sync="selectedPost"
></TableMenu>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/388474.html
標籤:Vue.js
