VueJS right way to edit prop without changing parent data

前端 未结 4 1300
傲寒
傲寒 2020-12-14 21:21

In my parent vue component I have a user object.

If I pass that user object to a child component as a prop:

         


        
4条回答
  •  情歌与酒
    2020-12-14 22:01

    you can have a data variable just with the information you want to be locally editable and load the value in the created method

    data() {
    return { localUserData: {name: '', (...)}
    }
    (...)
    created() {
        this.localUserData.name = this.user.name;
    }
    

    This way you keep it clear of which data you are editing. Depending on the need, you may want to have a watcher to update the localData in case the user prop changes.

提交回复
热议问题