Handling unexpected side effect in computed properties - VueJS

前端 未结 4 465
闹比i
闹比i 2021-01-07 23:12

In the following code, I\'m trying to use the getTranslation object to map values present in originalKeys array and push the values in a new array

4条回答
  •  温柔的废话
    2021-01-07 23:30

    I would advice you to move allKeys array to computed and get rid of unnecessary tableHeaders and getKeys:

    
    
    data(){
        return{
        selected: '',
        originalKeys: [],  //e.g. ["ALPHA_MIKE]
        getTranslation: {} //e.g. {"ALPHA_MIKE": "ALPHA MIKE"}
        }
    },
    computed: {
        allkeys() {
            return this.originalKeys.map(key => this.getTranslation[key])
        }
    }
    

    I'm not sure you need to assign this.selected = tableHeaders[0] since the first option will be chosen by default automatically.

提交回复
热议问题