vue router路由机制 别名,重定向,动态路由匹配

别名

/a的别名是/b,意味着,当用户访问/b时,URL会保持为/b,但是路由匹配则为/a,就像用户访问/a一样 使用alias设置别名

{
          
   
  		path: /teacher,
        name: teacher-a, //给路由命名方便跳转
        component: teacher,            
        alias: /student
                    },

此时访问teacher和student都是在访问teacher

重定向

“重定向”的意思是,当用户访问/a时,URL将会被替换成/b,然后匹配路由为/b,重定向与别名的区别在于。重定向会直接更改url,但别名不会。 使用redirect进行重定向 方法一:使用name来进行重定向

{
          
   
                        path: /teacher,
                        name: teacher, //给路由命名方便跳转
                        component: teacher,
                        redirect: {
          
   
                            name: "student"
                        },


                    }

方法二:使用地址重定向

{
          
   
       path: /teacher,
       name: teacher, //给路由命名方便跳转
       component: teacher,
       redirect: /student


                    }

动态路由匹配

我们经常需要把某种模式匹配到的所有路由,全都映射到同个组件。一个“路径参数”使用冒号:标记。当匹配到一个路由时,参数值会被设置到this.$route.params,可以在每个组件内使用

{
          
   
       path: /teacher/:id,
       name: teacher, //给路由命名方便跳转
       component: teacher,
       // redirect: /student


                    }

注意传值是必须使用的是param传值

经验分享 程序员 微信小程序 职场和发展