VueJS:如何在对象上调用数组方法`push`但仍然有效?

VueJS: how array method `push` can be called on an object but still works?

在下面的navigateToHome方法中,如何在对象($router)上使用Array的push方法Array.prototype.push,其中$router是主路由对象:

user.vue

<script>
    export default {
        data() {
            return {
                id: this.$route.params.id
            }
        },

        methods: {
            navigateToHome(){
                this.$router.push({path: '/'})
            }
        }
    }
</script>

main.js - $router 对象在这里定义

import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App.vue'
import { routes } from './routes'

Vue.use(VueRouter);

const router = new VueRouter({
    routes,
    mode: 'history'
})

new Vue({
  el: '#app',
  router,
  render: h => h(App)
})

只传递一个字符串而不是对象。你试过吗:这个?

this.$router.push('/')

因为不是Array.prototype.push方法,所以只是更新导航历史记录的方法。

push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
   this.history.push(location, onComplete, onAbort)
}

看一下源码: https://github.com/vuejs/vue-router/blob/dev/src/index.js#L141