$router:是全局的 router 实例。通过 vue 根实例中注入 router 实例,然后再注入到每个子组件,从而让整个应用都有路由功能。其中包含了很多属性和对象(比如 history 对象),任何页面也都可以调用其 push(), replace(), go() 等方法。
路由跳转:声明式导航、编程式导航
其中的push和replace的区别是push有历史记录,replace则没有
$route:对于当前的正在使用的路由的对象,可以获取path, name, params, query等属性
路由传参:
query参数:不需要占位 params参数:需要在接口的时候进行占位
第一种:字符串形式
this.$router.push("/search/" +this.keyword+"?k="+this.toUpperCase())
第二种:模板字符串
this.$router.push(`/search/${this.keyword}?k=${this.keyword.toUpperCase()}`)
第三种:对象
this.$router.push({name:"search",params:{keyword:this.keyword},query:{K:this.keyword.toUpperCase()}})