欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Angular Router 路由匹配 有大用

Angular Router 路由匹配

         
Photo by Roland Kay-Smith on Unsplash        

Angular Router 是 Angular 最重要的内置模块之一。

利用它可以非常快速的生成路由规则,实现前端的模块化开发。

但是其中有一些细节值得关注。

在本篇文章中,我会重点介绍 Angular Router 中——路由匹配的概念。

   

匹配原理

Angular 在匹配路由中的各个路径( path )的时候,

都会根据 ‘/’ ,把 URL拆解成一个 prefix 片和剩余 URL 片。

其中默认匹配算法,一旦 prefix 片与当前路径匹配,就能继续路由。

例子

假设我们在代码中定义了这样一个路由。

  1. const routes: Routes = [

  2.  { path: 'profile', component: ProfileComponent,

  3.    children:[

  4.      { path: 'child', component: ProfileChildComponent},

  5.     ]

  6.  },

  7.  { path: 'home', component: HomeComponent},

  8.  { path: '', pathMatch: 'prefix', redirectTo: '/home'},

  9.  { path: '**', redirectTo: '/profile'},  

  10. ];

当 Auglar 在处理URL—— ‘profile/child’时,

URL 总会被拆解成 ‘profile’(当做 prefix 片)和 ‘child’(剩余 URL 片)。

在默认 prefix 算法下,

Angular 会根据 prefix 片(’profile’)匹配到 ‘profile’ 这个路径,

并将剩余 URL —— ‘child’ 传入子路由,直到子路由也匹配成功。

这里就是一个 prefix 的例子。

陷阱

让我们在回过头来看看这个路由,重点关注 redirectTo 这个参数。

假如我们想让网页在空 URL 的时候显示 home 组件的内容,

且在其他 URL 的时候比如(@#@@/mistake/wrong )显示 profile 组件,

那么,上面定义的路由就会有一些问题。

  1. const routes: Routes = [

  2. ...

  3.  { path: '', pathMatch: 'prefix', redirectTo: '/home'},

  4.  { path: '**', redirectTo: '/profile'},  

  5. ...  

  6. ];

可以看到,我输入一串无意义的 URL 之后,页面并没有如期跳转到 profile ,

而是仍然跳转到了 home 组件。

这是因为空字符 ” 十分特殊,它是任何 URL 的 prefix,

在此情况下,若还是采用 prefix 进行匹配,

那任何无意义的路由都会匹配到这个空路径,从而导致结果与预期不符。

prefix 算法下,一旦 prefix 片与当前路径匹配,就能继续路由。

例子

为了解决这个问题,我们只需把匹配规则改成 ‘full’ 即可。

它会将当前路径下待处理的 URL 片作为一个整体,

与当前到达的路径进行比较。

让我们来看修改过的这个例子:

  1. const routes: Routes = [

  2. ...

  3.  { path: '', pathMatch: 'full', redirectTo: '/home'},

  4.  { path: '**', redirectTo: '/profile'},  

  5. ...  

  6. ];

假设现在有 URL—— @#@@ /mistake/wrong,到达路径 ” 时,

URL 会被切分成 ‘@#@@’ (prefix)和 ‘mistake/wrong ‘ (剩余URL)。

‘full’ 匹配算法会将待处理的 URL ——@#@@/mistake/wrong,

当做整体与空路径进行比较。

如此一来,那些无意义的路径就无法与空路径相匹配。

参考文献

Angular Router Set Up Redirects    

ANGULAR ROUTER: EMPTY PATHS, COMPONENTLESS ROUTES, AND REDIRECTS    

The Three Pillars of the Angular Router — Router States and URL Matching    


来自 https://www.imwhite.com.cn/2019/06/angular-router-match/




angular2路由器Routes配置里的pathMatch

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。                
本文链接:https://blog.csdn.net/kikyou_csdn/article/details/88033159                
           

angular2路由器Routes配置里的pathMatch

pathMatch是一个用来指定路由匹配策略的字符串。可选项有 prefix(默认值)和 full

prefix:以指定的路径开头;

full:与指定路径完全一样。

例1:

{

path: ‘abc’,

redirectTo: ‘/tabs/tab1’,

pathMatch: ‘prefix’

}

  • yes:

    /abc

    /abc/

    /abc/d

  • no:

    /abcd

    /abcd/

例2:

{

path: ‘abc’,

redirectTo: ‘/tabs/tab1’,

pathMatch: ‘full’

}

  • y:

    /abc

    /abc/

  • n:

    /abcd

    /abcd/


由于空路径是任何 url 的前缀,下面的配置表示任意路由都可以跳转:

{

path: ‘’,

redirectTo: ‘/tabs/tab1’,

pathMatch: ‘prefix’

}




angular路由的重定向,pathMatch='prefix'到底是干啥(学习angular5x路由笔记)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_41711593/article/details/79310903
具体路由知识官网上已经讲得很清楚,我就说的遇到的坑。

关于pathMatch='prefix'的用法

代码:

    {path:'a',redirectTo:'/dialog',pathMatch:'prefix'},
{path:'dialog',component:ConfirmDialog},
首先你应该了解,pathMatch='prefix'的官方意思是“剩下的URL以这个跳转路由中的prefix值开头时,就会匹配上这个跳转路由。
我就不理解其中的意思,于是我测试了多次,终于弄明白了意思:就是以上这个例子来说,他能帮助你把这些Url重定向。

1.http://localhost:9800/a

2.http://localhost:9800/a/sadf/adfs/

也就是说,只要以/a/开头的,都会被重定向。

不能重定向的:

1.http://localhost:9800/asdfsdf

2.http://localhost:9800/agsdgjklsgsdfsdf/sdfsdf

以/a开头,/a后面没有/的不能被识别。


来自  https://blog.csdn.net/weixin_41711593/article/details/79310903

普通分类: