RC.4 +溶液
你想要一个链接/ HTML还是你想要在代码中命令/命令?
链接:本RouterLink指令始终把提供的链接将作为delta为当前网址:
[routerLink]="['/absolute']"
[routerLink]="['../../parent']"
[routerLink]="['../sibling']"
[routerLink]="['./child']" // or
[routerLink]="['child']"
// with route param ../../parent;abc=xyz
[routerLink]="['../../parent', {abc: 'xyz'}]"
// with query param and fragment ../../parent?p1=value1&p2=v2#frag
[routerLink]="['../../parent']" [queryParams]="{p1: 'value', p2: 'v2'}" fragment="frag"
随着RouterLink,记得要导入和使用directives
阵列:
import { ROUTER_DIRECTIVES } from '@angular/router';
@Component({
directives: [ROUTER_DIRECTIVES],
命令:navigate()
方法需要一个起点(即,relativeTo
参数)。如果没有提供,导航是绝对的:
import { Router, ActivatedRoute } from '@angular/router';
...
constructor(private router: Router, private route: ActivatedRoute) {}
...
this.router.navigate("/absolute/path");
this.router.navigate("../../parent", {relativeTo: this.route});
this.router.navigate("../sibling", {relativeTo: this.route});
this.router.navigate("./child", {relativeTo: this.route}); // or
this.router.navigate("child", {relativeTo: this.route});
// with route param ../../parent;abc=xyz
this.router.navigate(["../../parent", {abc: 'xyz'}], {relativeTo: this.route});
// with query param and fragment ../../parent?p1=value1&p2=v2#frag
this.router.navigate("../../parent", {relativeTo: this.route,
queryParams: {p1: 'value', p2: 'v2'}, fragment: 'frag'});
// RC.5+: navigate without updating the URL
this.router.navigate("../../parent", {relativeTo: this.route, skipLocationChange: true});
请添加一些代码,如果 –