Javascript 使用更新本地数据渲染组件
我有一个带有道具和数据的组件。 组件接收到新的道具,我看到了道具,我影响了数据上的新道具,但是。。。没有渲染我的组件 资料来源:Javascript 使用更新本地数据渲染组件,javascript,vue.js,Javascript,Vue.js,我有一个带有道具和数据的组件。 组件接收到新的道具,我看到了道具,我影响了数据上的新道具,但是。。。没有渲染我的组件 资料来源: <template> <div> <label :dataField="(userSelected.email) ? userSelected.email :null" </label> </template> export default { props: {
<template>
<div> <label :dataField="(userSelected.email) ? userSelected.email :null" </label>
</template>
export default {
props: {
user: {
type: Object,
required: true,
default: null
},
data() {
return {
userSelected: this.$props.user,
}
},
watch: {
user(newProps){
console.log(" DATA = ", JSON.stringify(newProps))
console.log(" DATA = ", JSON.stringify(this.userSelected))
if ( JSON.stringify(newProps) !== JSON.stringify(this.userSelected) ) {
console.log("different props ")
this.userSelected = null;
this.userSelected = JSON.parse(JSON.stringify(newProps));
// the component is update but he doesn't render the html
}
}
},
}
关闭模板中的div
。另外,尝试this.user
而不是this.$props.user
,尽管这可能不相关。相同的结果=第一个le组件已装入ok,但如果我的道具用户已更新,则不会装入该组件,只是更新了user(newProps)
的预期用途是什么?它不包含在方法、观察或任何内容中。对不起,用户(newProps)是我的道具的观察者。我用这个来比较我的新道具你想实现什么?据我所知,userSelected
应该具有与user
相同的数据,在这种情况下,您是否最好使用userSelected
acomputed
属性?