2.6.10
https://codesandbox.io/s/vue-template-7hwty
$attrs
。渲染函数被调用一次,并且组件的$attrs
不变。
渲染调用两次和attrs
具有关键一样props
子组件将被删除。
我在项目自定义加载程序组件上创建了该组件,该组件必须处理延迟加载的组件的加载和网络错误,因为我无法使用https://vuejs.org/v2/guide/components-dynamic-async.html#Handling -Loading -以我为例。 通过不将下载的组件添加到反应性中(外部数据作为变量),可以忽略此错误。
使用$attrs
的副本: {...this.$attrs}
也可以解决问题
也许您应该使用道具来解决此问题:
render(h) {
console.log("Attributes of loader:", this.$attrs);
if (this.targetComponent) {
return h(this.targetComponent, {
props: this.$attrs // Change attrs to props,not `attrs: this.$attrs`
});
} else {
return h("div", "Component is loading");
}
}
因为你想拿道具
const targetComponent = {
props: {
// COMMENT PROP
someProp: {
type: String
}
},
render(h) {
console.log("Props of target", this.$props); // You can not get $props when render params only has attrs
return h("div", "WITH PROP:" + this.someProp);
}
};
创建attrs的复制和传递props都解决了我的问题,但是我仍然好奇为什么为什么父组件$attrs
会因为渲染包含相同键的props而改变呢?
这是因为$attrs
对象已重新分配。 我知道我已经在其他地方看到此问题:
重复的https://github.com/vuejs/vue/issues/10115