【发布时间】:2019-10-17 10:26:18
【问题描述】:
我已经学习 vue 几天了,我正在尝试在孩子和父母之间传递数据/道具。 现在我有了以下孩子:
<template>
<div>
<input v-model="name1" placeholder="string">
<input v-model="number1" placeholder="number">
<p v-text="name1"></p>
<p v-text="number1"></p>
</div>
</template>
<script>
export default {
name: "child",
props: {
name1 : String,
number1 : Number
}
}
</script>
然后是父母:
<template>
<div>
<child/>
</div>
</template>
<script>
import child from "@/components/complexComponent4/child.vue"
export default{
name: "parent",
components: {
child
}
}
</script>
现在,当我在输入字段中输入一些文本时,它会在段落中正确显示,因为绑定到段落的道具已更改。 但是,我收到此警告:
[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: "name1"
found in
---> <Child>
<Parent> at src/components/complexComponent4/parent.vue
<MyComplexView4.vue> at src/views/myComplexView4.vue
<App> at src/App.vue
<Root>
我在互联网上的多个地方以及文档中都读到了这个错误,我发现变异道具现在被认为是一种反模式: https://michaelnthiessen.com/avoid-mutating-prop-directly
不幸的是,我并没有真正找到任何具体的和/或有用的信息来解决这个问题。特别是在 vue 以不同方式处理原始数据和对象/数组的上下文中(对象/数组通过引用传递)。
v-model 似乎在利用 vue 的力量方面发挥了重要作用,因为它支持双向绑定。因此,我不想完全忽略它,除非它的使用变得如此困难以至于无法证明收益是合理的。
【问题讨论】:
标签: vue.js