我使用angular2 +路由器,我正在尝试匹配以/tree
开头的任何路径,例如:
example.com/projects/1/repository/tree/src
example.com/projects/1/repository/tree/src/app
example.com/projects/1/repository/tree/src/app/core
等等。
我尝试了这种方法,但它不起作用:https://github.com/angular-ui/ui-router/wiki/URL-Routing#regex-parameters
我的路由器配置:
const routes: Routes = [
{
path: 'projects/:id/repository/{tree:.*}',
component: FilesListComponent
}
tree
之后的所有内容我需要在控制器中捕获一个参数,因此我可以用它进行API调用。
答案 0 :(得分:0)
这是解决方案试试这个..
import { Component } from '@angular/core';
import { Router } from "@angular/router";
import { Event as NavigationEvent } from "@angular/router";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
public url: string;
constructor(router: Router) {
router.events.subscribe((event: NavigationEvent): void => {
this.url = router.url;
if (this.url.match('/tree/')) {
console.log("Matched your Path",this.url);
}
}
);
}
}
你可以在这里改变你的匹配部分
if(this.url.match('give your matching here')){
//do something
}
您可以在this.url
变量中获取捕获的值。
它完美无缺。我希望能帮到你。