Is there a proper way of resetting a component's initial data in vuejs?

后端 未结 5 1375
遥遥无期
遥遥无期 2020-11-29 18:07

I have a component with a specific set of starting data:

data: function (){
    return {
        modalBodyDisplay: \'getUserInput\', // possible values: \'ge         


        
5条回答
  •  遥遥无期
    2020-11-29 18:32

    If you are annoyed by the warnings, this is a different method:

    const initialData = () => ({})
    
    export default {
      data() {
        return initialData();
      },
      methods: {
        resetData(){
          const data = initialData()
          Object.keys(data).forEach(k => this[k] = data[k])
        }
      }
    }

    No need to mess with $data.

提交回复
热议问题