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

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

    I had to reset the data to original state inside of a child component, this is what worked for me:

    Parent component, calling child component's method:

         <button @click="$refs.childComponent.clearAllData()">Clear All</button >
    
         <child-component ref='childComponent></child-component>
    

    Child component:

    1. defining data in an outside function,
    2. assigning the data object to and outside function
    3. defining the clearallData() method that is to be called upon by the parent component

      function initialState() {
         return { 
           someDataParameters : '',
           someMoreDataParameters: ''
                }
        }
      
      export default {
         data() {
          return initialState();
        },
          methods: {
        clearAllData() {
        Object.assign(this.$data, initialState());
      },
      
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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());
        }
    }

    0 讨论(0)
  • 2020-11-29 18:41

    To reset component data in a current component instance you can try this:

    Object.assign(this.$data, this.$options.data())
    

    Privately I have abstract modal component which utilizes slots to fill various parts of the dialog. When customized modal wraps that abstract modal the data referred in slots belongs to parent component scope. Here is option of the abstract modal which resets data every time the customized modal is shown (ES2015 code):

    watch: {
        show (value) { // this is prop's watch
          if(value) {
            Object.assign(this.$parent.$data, this.$parent.$options.data())
          }
        }
    }
    

    You can fine tune your modal implementation of course - above may be also executed in some cancel hook.

    Bear in mind that mutation of $parent options from child is not recommended, however I think it may be justified if parent component is just customizing the abstract modal and nothing more.

    0 讨论(0)
  • 2020-11-29 18:48

    Caution, Object.assign(this.$data, this.$options.data()) does not bind the context into data().

    So use this:

    Object.assign(this.$data, this.$options.data.apply(this))

    cc this answer was originally here

    0 讨论(0)
提交回复
热议问题