Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度路由器-如何创建没有正斜杠的动态路由_Javascript_Angular - Fatal编程技术网

Javascript 角度路由器-如何创建没有正斜杠的动态路由

Javascript 角度路由器-如何创建没有正斜杠的动态路由,javascript,angular,Javascript,Angular,这是我的路由数组。我想要任何带-的路由使用第一个组件,任何其他路由使用第二个组件 [ { path: 'the-:param', component: MyComponent }, { path: ':param', component: MyOtherComponent } ] 有什么办法可以做到这一点。使用angular 7.当路径和路径匹配的组合不够表达时,可以提供自定义URL匹配器 fu

这是我的路由数组。我想要任何带-的路由使用第一个组件,任何其他路由使用第二个组件

[
    { 
        path: 'the-:param',
        component: MyComponent
    },
    { 
        path: ':param',
        component: MyOtherComponent
    }
]

有什么办法可以做到这一点。使用angular 7.

当路径和路径匹配的组合不够表达时,可以提供自定义URL匹配器

function leadingtThe(url: UrlSegment[]) {
  return url.length === 1 && url[0].path.startsWith('the-') ? ({consumed: url}) : null;
}

const routes: Routes = [{ matcher: leadingtThe, component: MyComponent }];
这应该匹配传入路径中的任何前导
和-

url.length===1
在这里输入此参数是为了确保url只有一个段,如果它不止一个段,则函数返回null且不匹配

如果您想匹配任何以
开头的url-
,即使它有多个段,例如
localhost:4200/anything/segment/someothersegment
那么应该是:

function leadingtThe(url: UrlSegment[]) {
  return url[0].path.startsWith('the-') ? ({consumed: url}) : null;
}

const routes: Routes = [{ matcher: leadingtThe, component: MyComponent }];

写一个自定义路由器模块,这样你就可以实现你的规则而不是Angular的。谢谢-没有办法用当前的Angular路由器来实现吗?听起来需要做很多工作。正在寻找一个更易于维护的解决方案。URLSEMENT-正是我想要的-感谢Mahmoud的示例!