vue中$router以及$route的使用

路由基本概念

  • route,它是一条路由。
{ path: ‘/home‘, component: Home }
  • routes,是一组路由。
const routes = [
  { path: ‘/home‘, component: Home },
  { path: ‘/about‘, component: About }
]
  • router可以理解为一个容器,或者说一种机制,它管理了一组route。简单来说,route只是进行了URL和函数的映射,而在当接收到一个URL之后,去路由映射表中查找相应的函数,这个过程是由router来处理的。
    const router = new VueRouter({
          routes // routes: routes 的简写
    })

VUE中

  • $route为当前router跳转对象里面可以获取name、path、query、params等。
  • $router为VueRouter实例,想要导航到不同URL,则使用$router.push方法,返回上一个history使用$router.go方法。跟上面说的一样,这里的$router就管理路由的跳转,英文里er结尾的都表示一种人,这里你可以把这个想象中一个管理者,他来控制路由去哪里(push、go),这样就比较容易记。

路由跳转

  • 1 可以手写完整的path:
this.$router.push({path:`/user/${userId}`})

这种方式需要在路由中作如下配置

{
     path: ‘/user/:userId‘,
     name: ‘***‘,
     component: ***
   }

这种接收参数的方式是this.$route.params.userId。

  • 2 也可以用params传递:
this.$router.push({name:‘Login‘,params:{id:‘leelei‘}})
//不带参数 变成 ip/login
  • 3 也可以用query传递:
this.$router.push({path:‘/login‘,query:{id:‘leelei‘})
//带查询参数变成 ip/login?id=leelei
//带斜杠/代表从根目录出发,不会嵌套之前的路径

query传参是针对path的,params传参是针对name的。。接收参数的方式都差不多。。this.$route.query.和this.$route.params.

注意这只是跳转url,跳转到这个url显示什么组件,得配置路由。router跳转和<router-link>标签跳转,规则差不多。

一些需要注意的事

  • 使用query传参的话,会在浏览器的url栏看到传的参数类似于get请求,使用params传参的话则不会,类似于post请求。
  • 如果提供了path,params将会被忽略(即如果要使用params传参,则一定要使用name),但是query不属于这种情况。如果使用完整路径和query传参,刷新页面时不会造成路由传参的参数丢失。
  • router.push和router.replace的区别是:replace不会向 history 添加新记录,而是替换掉当前的 history 记录,即使用replace跳转到的网页‘后退’按钮不能点击。

最后

谢谢大家,如果有错误的地方请指出。

阅读 7.2k更新于 2019-12-28

原文地址:https://www.cnblogs.com/sexintercourse/p/12258169.html

时间: 2024-08-30 10:34:20

vue中$router以及$route的使用的相关文章

阿里矢量图的应用--flex布局--vue中$router和$route的方法

1.阿里矢量图字体图标的用法 2.flex布局 display:flex:设置父容器为伸缩盒子,会使每一个子元素自动变成伸缩项 接着设置子元素主轴方向上的排列方式 justify-content: flex-start让子元素从父容器的起始位置开始排列: flex-end:让子元素从父容器的结束位置开始排列: ? center:让子元素从父容器的中间位置开始排列: ? space-between:左右对齐父容器的开始和结束,中间平均分页,产生相同的间距: ? space-around:将多余的空

vue中router以及route的使用

路由基本概念 route,它是一条路由. { path: '/home', component: Home } routes,是一组路由. const routes = [ { path: '/home', component: Home }, { path: '/about', component: About } ] router可以理解为一个容器,或者说一种机制,它管理了一组route.简单来说,route只是进行了URL和函数的映射,而在当接收到一个URL之后,去路由映射表中查找相应的函

vue中$router和$route的区别

$router是VueRouter的实例,在script标签中想要导航到不同的URL,使用$router.push方法. 返回上一个历史history用$router.to(-1) $route为当前router跳转对象.里面可以获取当前路由的name,path,query,parmas等. 嗯,就酱~~ 参考:https://www.cnblogs.com/yangguoe/p/9974793.html 原文地址:https://www.cnblogs.com/jin-zhe/p/105064

[Vue] Code split by route in VueJS

In this lesson I show how to use webpack to code split based on route in VueJS. Code splitting is a useful tool to help eliminate unused code and only load what's necessary. Additional Resources https://router.vuejs.org/en/advanced/lazy-loading.html

彻底搞懂$router 和 $route

之前,一直对$router和$route之间的区别比较模糊,后来结合网上的博客和官方给出的解释了解了他们之间的区别. 1.router是VueRouter的一个对象,通过Vue.use(VueRouter)和VueRouter构造函数得到一个router的实例对象,这个对象中是一个全局的对象,他包含了所有的路由包含了许多关键的对象和属性. 举例: 1.$router.push({path:'home'});本质是向history栈中添加一个路由并且添加一个history记录,在我们看来是 切换路

Vue用router.push(传参)跳转页面,参数改变,跳转页面数据不刷新的解决办法

vue-router同路由$router.push不跳转一个简单解决方案 vue-router跳转一般是这么写: goPage(ParentDeptCode2,DeptCode2,hosName,hosId){ this.$router.push({ path:'/ChoiceTime', query:{ DeptCode:ParentDeptCode2, DeptCode2:DeptCode2, hosName:hosName, hosId:hosId } })} 但是当遇到,需要跳转同页面不

vue路由-router

VueRouter基础 vue路由的注册 导入 <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script> # 可以是下载之后的 <script src="vue.min.js"></script> <script src="vue-router.js"></script> 定义一个匹配规则对

[Angular2 Router] Optional Route Query Parameters - The queryParams Directive and the Query Parameters Observable

In this tutorial we are going to learn how to use the Angular 2 router to pass optional query parameters from one route into another route. There are couple of ways of doing this from the source route perspective: we use the queryParams property in t

[Angular2 Router] CanActivate Route Guard - An Example of An Asynchronous Route Guard

In this tutorial we are going to learn how we can to configure an can activate route guard in the Angular 2 router. We are going to implement the concrete example where a user can only enter a certain route if its authorized to do so. We are also goi