Vue prop being mutated 是指在使用 Vue 的过程中,当一个 prop 被修改时,会发出一个警告。这是因为 Vue 不允许在组件内部修改 props,而是要求组件只能读取 props,而不能修改它们。
Vue 将 props 看作是外部传入的数据,它们应该是只读的。如果你尝试在组件内部修改 props,Vue 将会发出一个警告:“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:”
这意味着你不应该直接修改 props 的值,而应该使用 data 或 computed 属性来根据 props 的值来获得新的值。这样做的好处是当父组件重新渲染时,props 的值将不会被覆盖。
// 例子1: 错误的方式 props: { myProp: String, }, methods: { updateMyProp() { // 错误! 不能直接修改prop的值! this.myProp = 'new value'; // 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: "myProp" }, }, // 例子2: 正确的方式 props: { myProp: String, }, data() { return { newValue: this.myProp }; }, methods: { updateMyProp() { this.newValue = 'new value'; }, },
该页面假设你已经阅读过了组件基础。如果你还对组件不太了解,推荐你先阅读它。
一个非 prop 的 attribute 是指传向一个组件,但是该组件并没有相应 props 或 emits 定义的 attribute。常见的示例包括 class
、style
和 id
属性。
当组件返回单个根节点时,非 prop attribute 将自动添加到根节点的 attribute 中。例如,在 <date-picker>
组件的实例中:
app.component("date-picker", {
template: `
<div class="date-picker">
<input type="datetime" />
</div>
`
})
如果我们需要通过 data status
property 定义 <date-picker>
组件的状态,它将应用于根节点 (即 div.date-picker
)。
<!-- 具有非prop attribute的Date-picker组件-->
<date-picker data-status="activated"></date-picker>
<!-- 渲染 date-picker 组件 -->
<div class="date-picker" data-status="activated">
<input type="datetime" />
</div>
同样的规则适用于事件监听器:
<date-picker @change="submitChange"></date-picker>
app.component("date-picker", {
created() {
console.log(this.$attrs) // { onChange: () => {} }
}
})
当有一个 HTML 元素将 change
事件作为 date-picker
的根元素时,这可能会有帮助。
app.component("date-picker", {
template: `
<select>
<option value="1">Yesterday</option>
<option value="2">Today</option>
<option value="3">Tomorrow</option>
</select>
`
})
在这种情况下,change
事件监听器从父组件传递到子组件,它将在原生 select
的 change
事件上触发。我们不需要显式地从 date-picker
发出事件:
<div id="date-picker" class="demo">
<date-picker @change="showChange"></date-picker>
</div>
const app = Vue.createApp({
methods: {
showChange(event) {
console.log(event.target.value) // 将记录所选选项的值
}
}
})
如果你不希望组件的根元素继承 attribute,你可以在组件的选项中设置 inheritAttrs: false
。例如:
禁用 attribute 继承的常见情况是需要将 attribute 应用于根节点之外的其他元素。
通过将 inheritAttrs
选项设置为 false
,你可以访问组件的 $attrs
property,该 property 包括组件 props
和 emits
property 中未包含的所有属性 (例如,class
、style
、v-on
监听器等)。
使用上一节中的 date-picker 组件示例,如果需要将所有非 prop attribute 应用于 input
元素而不是根 div
元素,则可以使用 v-bind
缩写来完成。
app.component("date-picker", {
inheritAttrs: false,
template: `
<div class="date-picker">
<input type="datetime" v-bind="$attrs" />
</div>
`
})
有了这个新配置,data status
attribute 将应用于 input
元素!
<!-- Date-picker 组件 使用非 prop attribute -->
<date-picker data-status="activated"></date-picker>
<!-- 渲染 date-picker 组件 -->
<div class="date-picker">
<input type="datetime" data-status="activated" />
</div>
与单个根节点组件不同,具有多个根节点的组件不具有自动 attribute 回退行为。如果未显式绑定 $attrs
,将发出运行时警告。
<custom-layout id="custom-layout" @click="changeValue"></custom-layout>
// 这将发出警告
app.component("custom-layout", {
template: `
<header>...</header>
<main>...</main>
<footer>...</footer>
`
})
// 没有警告,$attrs被传递到<main>元素
app.component("custom-layout", {
template: `
<header>...</header>
<main v-bind="$attrs">...</main>
<footer>...</footer>
`
})
该页面假设你已经阅读过了组件基础。如果你还对组件不太了解,推荐你先阅读它。#在动态组件上使用 keep-alive我们之前曾经在一个...
Vue 的过渡系统提供了非常多简单的方法设置进入、离开和列表的动效。那么对于数据元素本身的动效呢,比如:数字和运算颜色的显示...
Vue 推荐在绝大多数情况下使用模板来创建你的 HTML。然而在一些场景中,你真的需要 JavaScript 的完全编程的能力。这时你可以用...
本节使用单文件组件代码示例的语法本指南假定你已经阅读了组合式 API 简介和响应性原理。如果你不熟悉组合式 API,请先阅读这篇...
本指南假定你已经阅读了 Provide / Inject、组合式 API 介绍和响应性基础。如果你不熟悉组合式 API,请先阅读这篇文章。我们也可...