my application is working fine, but here is the issue where I get an error, when I click on any of the menu, I get the following error, please help. good work.
[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "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>
TableMenu.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>
CodePudding user response:
v-on:click="selectedPost = post"
is the culprit; selectedPost
is a prop here and you cannot assign to a prop.
There are two different solutions depending on what you want:
- Make
selectedPost
a local data property instead of a prop. You can then modifyselectedPost
but since it is no longer a prop, you cannot acceptselectedPost
from the parent anymore (but you're not really doing that anyway).
data() {
return {
selectedPost: null
}
}
- Instead of modifying
selectedPost
directly, you should emit an event which the parent can handle to update its data. The convention is to emit an event named likeupdate:selectedPost
which will make it work with the.sync
modifier.
Change the click handler to:
@click="$emit('update:selectedPost', post)"
Then in the parent, update as follows:
<TableMenu
:posts="posts"
:selectedPost="selectedPost"
@update:selectedPost="selectedPost = $event"
></TableMenu>
Or you can use .sync
to make the above a bit simpler (it does the same thing):
<TableMenu
:posts="posts"
:selectedPost.sync="selectedPost"
></TableMenu>