首先为什么这样用...mapgetters

mounted(){
this.init()
},
methods: {

fn () {

return {

a: 1, b: 2

}

},

init () {

console.log({

...this.fn(),

c: 3,

d: 4

})

}

}

仿照Vuex中的mapGetters

<script>

const getters = {

a: () => 1,

b: () => 2

}

function fn (keys) {

const data = {}

keys.forEach(key => {

if (getters.hasOwnProperty(key)) {

data[key] = getters[key]

}

})

return data

}

export default {

data () {

return {

msg: 'hello'

}

},

computed: {
...fn(['a', 'b', 'c']),

},

mounted () {

console.log(this.a, this.b)

} 

}

  

</script>

image.png


HappyCodingTop
526 声望847 粉丝

Talk is cheap, show the code!!


下一篇 »
Symbol