【发布时间】:2021-12-23 04:16:24
【问题描述】:
我有父组件,我想在子组件中发送一个布尔值,以显示带有概览的 v-dialog。 结果是打开了许多 v-dialog,并在 console.log 中出现了一条错误消息。 我不知道我错在哪里。 你能帮助我吗?谢谢
<template>
<div>
<div class="container">
<v-card
v-for="el of listTv"
:key="el.id"
class="card"
:style="{
backgroundImage: `url('https://image.tmdb.org/t/p/w500/${el.backdrop_path}')`,
backgroundSize: 'auto',
backgroundPosition: 'center',
}"
>
<v-card-title class="textTitle">{{ el.name }}</v-card-title>
<h5 class="genres">
{{ el.genres[0] === undefined ? "Nessun genere" : el.genres[0].name }}
</h5>
<v-btn color="primary" class="btn" @click="dialog = true"
>Trama...</v-btn
>
</v-card>
</div>
<DialogsOverview
v-for="element in listTv"
:key="element.id"
:overView="element.overview || 'Nessuna descrizione'"
:title="element.name"
:dialog="dialog"
/>
</div>
</template>
<script>
import DialogsOverview from "./DialogsOverview.vue";
export default {
components: { DialogsOverview },
props: ["listTv"],
data() {
return {
dialog: false,
};
},
methods: {},
};
</script>
<style>
@import url("./style/CardsMovie.css");
</style>
子组件:
<template>
<div>
<v-dialog v-model="dialog">
<v-card>
<v-card-title>{{ title }}</v-card-title>
<v-card-text>{{ overView }}</v-card-text>
</v-card>
</v-dialog>
</div>
</template>
<script>
export default {
props: {
dialog: Boolean,
overView: String,
title: String,
},
data() {
return {};
},
methods: {},
};
</script>
<style></style>
错误控制台
vue.runtime.esm.js?2b0e:619 [Vue 警告]:避免改变 prop 直接因为值将被覆盖每当父 组件重新渲染。相反,使用基于数据或计算属性 关于道具的价值。正在变异的道具:“对话”
found in ---> <DialogsOverview> at src/components/DialogsOverview.vue <CardsMovie> at src/components/CardsMovie.vue <Main> at src/components/Main.vue <VMain> <VApp> <App> at src/App.vue <Root>
【问题讨论】:
-
您的代码以这样的方式编写,即 v-model="dialog" 将能够更改道具,道具应该是只读的。
-
我该如何解决?
标签: javascript vue.js vuejs2 vue-props