[英]How to redirect to a route on ngOnit?
I am trying to redirect to a route when user directly paste the url. 当用户直接粘贴网址时,我试图重定向到路由。
I want to direct the user to /en/sell page on hitting the url 我想引导用户访问网址/ en / sell页面
http://localhost:3000/en/sell/confirmation
here is my code, 这是我的代码,
ngOnInit() {
this.storedListing = this.sellerFlow.getSellerFlowObject();
if (this.storedListing === null) {
this.router.navigate(['./']);
}
}
i can see this.router getting executed but the application does not have any change. 我可以看到this.router正在执行,但应用程序没有任何更改。 what is the issue?
有什么问题?
You have to use ../
if you want to go from /en/sell/confirmation
to /en/sell/
: 如果要从
/en/sell/confirmation
转到/en/sell/
则必须使用../
:
this.router.navigate(['../']);
See the documentation . 请参阅文档 。
You could instead redirect from your routing: 您可以改为从路由重定向:
{
path: 'en/sell/confirmation',
redirectTo: 'en/sell',
pathMatch: 'full'
},
import {OnInit} from '@angular/core';
import {Router} from '@angular/router';
export class AddDisplay {
constructor(private router: Router){}
ngOnInit() {
this.router.navigate(['./SomewhereElse']);
}
}
You have to specify the relativeTo
param, as below: 您必须指定
relativeTo
参数,如下所示:
import { ActivatedRoute, Router } from '@angular/router';
constructor(private route: ActivatedRoute, private router: Router) { }
ngOnInit() {
this.storedListing = this.sellerFlow.getSellerFlowObject();
if (this.storedListing === null) {
this.router.navigate(['../'], { relativeTo: this.route });
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.