Vuejs2 VueJS:如何在对象上调用数组方法“push”,但仍然有效?
在以下Vuejs2 VueJS:如何在对象上调用数组方法“push”,但仍然有效?,vuejs2,javascript-objects,vue-router,Vuejs2,Javascript Objects,Vue Router,在以下navigateToHome方法中,如何在对象($router)上使用数组的推送方法Array.prototype.push,其中$router是主路由器对象: user.vue <script> export default { data() { return { id: this.$route.params.id } }, methods
navigateToHome
方法中,如何在对象($router
)上使用数组的推送方法Array.prototype.push
,其中$router
是主路由器对象:
user.vue
<script>
export default {
data() {
return {
id: this.$route.params.id
}
},
methods: {
navigateToHome(){
this.$router.push({path: '/'})
}
}
}
</script>
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)
}
请看一下源代码:
@ghybs所以你的意思是这里使用的不是
数组.prototype.push
方法,而是vue路由器
有内置的推送方法??