首先爲何這樣用...mapgettersthis
mounted(){ this.init() }, methods: { fn () { return { a: 1, b: 2 } }, init () { console.log({ ...this.fn(), c: 3, d: 4 }) } }
<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>