为什么这个 Vue prop 对变化没有反应?

Ant*_*ntG 3 javascript vue.js

我的组件中有一个 Prop,它是一个 User 对象,然后我有这个函数:

    onChange: function(event){
        this.$v.$touch();
        if (!this.$v.$invalid) {
            this.$axios.put('update',{
                code:this.user.code,
                col:event.target.name,
                val:event.target.value  
            }).then(response => {
                this.user[event.target.name]=event.target.value
            });
        }
    }
Run Code Online (Sandbox Code Playgroud)

我可以在 Vue 控制台调试器中看到正确的属性已更新,该属性在创建组件时存在,但我引用它的模板不会刷新:

<p>Hi {{user.nickname}}, you can edit your details here:</p>
Run Code Online (Sandbox Code Playgroud)

这些都在同一个组件中,因此我不会导航到子组件或父组件。我确定 props 在我的代码中的其他地方已经反应了?

Ant*_*ntG 7

好吧,看来这是有意的行为。根据文档\n https://v2.vuejs.org/v2/guide/components-props.html在我拥有的场景中应将其处理为:

\n
\n
    \n
  1. prop用于传入一个初始值;之后子组件希望将其用作本地数据属性。在这种情况下,\n\n\xe2\x80\x99 最好定义一个使用 prop 作为其初始值的本地数据属性:

    \n
     props: [\'initialCounter\'],\n data: function () {\n   return {\n     counter: this.initialCounter\n   }\n }\n
    Run Code Online (Sandbox Code Playgroud)\n
  2. \n
\n
\n