1.this.$ router
this.$ router 访问路由器 ;
this.$router 是一个全局的路由器对象,包含了很多属性和对象(比如 history 对象),任何页面都可以调用其 push(), replace(), go() ,resolve()等方法。
- this.$ router.resolve。
vue 中新窗口打开vue页面 (this.$router.resolve)
outsideLink () {
let {href}= this.$router.resolve({
path: "/newLinkPage",
});
window.open(href, '_blank');
}
2.this.$ router.go(n),
该方法采用一个整数作为参数,表示在历史堆栈中前进或后退多少步,类似window.history.go(n)。 this.$ router.go(1)前进一步
this.$ router.go(-1)后退一步
3.this.$ router.push()
// 字符串
this.$ router.push('home')
// 命名的路由
router.push({ name: 'user', params: { userId: 123 }})
// 带查询参数,变成/backend/order?selected=2
this.$router.push({path: '/backend/order', query: {selected: "2"}});
this.$router.push同时中同时有path跟name 的时候生效哪个变量?面试的时候遇到过这种问题。在项目中用不上。记得答案好像是生效path
2.this.$ route
this.$ route 访问当前路由 ;
this.$ route 表示当前路由对象,每一个路由都会有一个 route 对象,是一个局部的对象,可以获取对应的 name, path, params, query 等属性。