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

后端 未结 5 1376
遥遥无期
遥遥无期 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:34

    1. extract the initial data into a function outside of the component
    2. use that function to set the initial data in the component
    3. re-use that function to reset the state when needed.

    // outside of the component:
    function initialState (){
      return {
        modalBodyDisplay: 'getUserInput', 
        submitButtonText: 'Lookup', 
        addressToConfirm: null,
        bestViewedByTheseBounds: null,
        location:{
          name: null,
          address: null,
          position: null
        }
      }
    }
    
    //inside of the component:
    data: function (){
        return initialState();
    } 
    
    
    methods:{
        resetWindow: function (){
            Object.assign(this.$data, initialState());
        }
    }

提交回复
热议问题