如何在新路由器 Angular 中生成 url 路径 2
How to generate url path in new Router Angular 2
新路由器 3.0.0 中的模拟 deprecated-router
方法 generate
是什么?
早期可能需要这样的东西:
this._router.generate(['Profile']).urlPath;
如何在新路由器上实现?
https://angular.io/docs/ts/latest/api/router/index/Router-class.html#!#createUrlTree-anchor
var urlTree = this._router.createUrlTree(['Profile']);
您可以将结果传递给
this._router.navigate(/*string|UrlTree);
或获取 URL
var url = this._router.createUrlTree(['Profile']).toString();
Edit:从 Angular 6 开始,您将结果传递给 NavigateByUrl:
this._router.navigateByUrl(string|UrlTree);
这些是 Günter 引用的页面中的当前示例
// create /team/33/user/11
router.createUrlTree(['/team', 33, 'user', 11]);
// create /team/33;expand=true/user/11
router.createUrlTree(['/team', 33, {expand: true}, 'user', 11]);
// you can collapse static segments like this (this works only with the first passed-in value):
router.createUrlTree(['/team/33/user', userId]);
// If the first segment can contain slashes, and you do not want the router to split it, you
// can do the following:
router.createUrlTree([{segmentPath: '/one/two'}]);
// create /team/33/(user/11//right:chat)
router.createUrlTree(['/team', 33, {outlets: {primary: 'user/11', right: 'chat'}}]);
// remove the right secondary node
router.createUrlTree(['/team', 33, {outlets: {primary: 'user/11', right: null}}]);
// assuming the current url is `/team/33/user/11` and the route points to `user/11`
// navigate to /team/33/user/11/details
router.createUrlTree(['details'], {relativeTo: route});
// navigate to /team/33/user/22
router.createUrlTree(['../22'], {relativeTo: route});
// navigate to /team/44/user/22
router.createUrlTree(['../../team/44/user/22'], {relativeTo: route});
https://angular.io/docs/ts/latest/api/router/index/Router-class.html#!#createUrlTree-anchor
新路由器 3.0.0 中的模拟 deprecated-router
方法 generate
是什么?
早期可能需要这样的东西:
this._router.generate(['Profile']).urlPath;
如何在新路由器上实现?
https://angular.io/docs/ts/latest/api/router/index/Router-class.html#!#createUrlTree-anchor
var urlTree = this._router.createUrlTree(['Profile']);
您可以将结果传递给
this._router.navigate(/*string|UrlTree);
或获取 URL
var url = this._router.createUrlTree(['Profile']).toString();
Edit:从 Angular 6 开始,您将结果传递给 NavigateByUrl:
this._router.navigateByUrl(string|UrlTree);
这些是 Günter 引用的页面中的当前示例
// create /team/33/user/11
router.createUrlTree(['/team', 33, 'user', 11]);
// create /team/33;expand=true/user/11
router.createUrlTree(['/team', 33, {expand: true}, 'user', 11]);
// you can collapse static segments like this (this works only with the first passed-in value):
router.createUrlTree(['/team/33/user', userId]);
// If the first segment can contain slashes, and you do not want the router to split it, you
// can do the following:
router.createUrlTree([{segmentPath: '/one/two'}]);
// create /team/33/(user/11//right:chat)
router.createUrlTree(['/team', 33, {outlets: {primary: 'user/11', right: 'chat'}}]);
// remove the right secondary node
router.createUrlTree(['/team', 33, {outlets: {primary: 'user/11', right: null}}]);
// assuming the current url is `/team/33/user/11` and the route points to `user/11`
// navigate to /team/33/user/11/details
router.createUrlTree(['details'], {relativeTo: route});
// navigate to /team/33/user/22
router.createUrlTree(['../22'], {relativeTo: route});
// navigate to /team/44/user/22
router.createUrlTree(['../../team/44/user/22'], {relativeTo: route});
https://angular.io/docs/ts/latest/api/router/index/Router-class.html#!#createUrlTree-anchor