需要刷新页面以使路由器导航再次工作?
Need to refresh page for router navigate to work again?
我有一个重定向到 post 创建页面的按钮,但由于某种原因,它只能运行一次。之后我必须刷新页面才能让它再次工作。
运行 Angular2,我相信我已经用相对路径尝试了整个过程..
selectCreateData() {
if (this.myView.link) {
this.router.navigate([this.myView.link, 'create']);
} else {
this.createDataValue = {};
}
}
<button (click)="selectCreateData()">Create new Post</button>
控制台中没有错误,记录 this.myView.link
returns localhost
之后的整个路径。示例:/7f45c1b1-b0c4-4f2d-96fd-a786889fbae2/projects/2723ec7d-3d2b-41f3-b332-b2e384a8e03a/process/
为了再次路由到 "same" 路由,您需要结合使用 onSameUrlNavigation
和订阅路由器事件。
第 1 步
onSameUrlNavigation Define what the router should do if it receives a navigation request to the current URL. (see https://angular.io/api/router/Router#properties)
您将使用 ExtraOptions
参数为您的 .forRoot([routes], { onSameUrlNavigation: 'reload' }
应用它
第 2 步
在您的组件中监听 NavigationEnd
class(es):
export class Test1Component {
public constructor(private router: Router) {
this.router.events.subscribe((evt) => {
if (evt instanceof NavigationEnd) {
console.log(`${ new Date().toString() }: test1 component routed to`);
}
});
}
}
示例:
有关完整的工作示例,请参阅 https://github.com/mateothegreat/ng-byexamples-routing-same。
我有一个重定向到 post 创建页面的按钮,但由于某种原因,它只能运行一次。之后我必须刷新页面才能让它再次工作。
运行 Angular2,我相信我已经用相对路径尝试了整个过程..
selectCreateData() {
if (this.myView.link) {
this.router.navigate([this.myView.link, 'create']);
} else {
this.createDataValue = {};
}
}
<button (click)="selectCreateData()">Create new Post</button>
控制台中没有错误,记录 this.myView.link
returns localhost
之后的整个路径。示例:/7f45c1b1-b0c4-4f2d-96fd-a786889fbae2/projects/2723ec7d-3d2b-41f3-b332-b2e384a8e03a/process/
为了再次路由到 "same" 路由,您需要结合使用 onSameUrlNavigation
和订阅路由器事件。
第 1 步
onSameUrlNavigation Define what the router should do if it receives a navigation request to the current URL. (see https://angular.io/api/router/Router#properties)
您将使用 ExtraOptions
参数为您的 .forRoot([routes], { onSameUrlNavigation: 'reload' }
第 2 步
在您的组件中监听 NavigationEnd
class(es):
export class Test1Component {
public constructor(private router: Router) {
this.router.events.subscribe((evt) => {
if (evt instanceof NavigationEnd) {
console.log(`${ new Date().toString() }: test1 component routed to`);
}
});
}
}
示例:
有关完整的工作示例,请参阅 https://github.com/mateothegreat/ng-byexamples-routing-same。