[英]Angular 7 router.navigate with message after redirecting
I'm using this code to redirect this.router.navigate(['/abc']);
我正在使用此代码重定向this.router.navigate(['/abc']);
. . How to show some message on the new page using <ngb-alert>
, Is it possible to pass some variable while redirecting but not as url query parameters?如何使用<ngb-alert>
在新页面上显示一些消息,是否可以在重定向时传递一些变量但不能作为 url 查询参数?
You can do that with State
as below:您可以使用State
执行此操作,如下所示:
When you want to navigate set state
:当您要导航时设置state
:
const navigationExtras: NavigationExtras = {state: {data: 'This is an example'}};
this.router.navigate(['/abc'], navigationExtras);
In destination component you can get data like the following:在目标组件中,您可以获得如下数据:
data:string;
constructor(private router: Router) {
const navigation = this.router.getCurrentNavigation();
const state = navigation.extras.state as {data: string};
this.data = state.data;
}
You can use a service to pass data from one component to another without using route parameters at all.您可以使用服务将数据从一个组件传递到另一个组件,而完全不使用路由参数。
Their is already a datailed topic about this.他们已经是一个关于这个的数据化话题。 Send data through routing paths in Angular 通过 Angular 中的路由路径发送数据
Angular does not provide a way to send data with navigate funvtion u can used a hack insted Angular 不提供使用导航功能发送数据的方法,您可以使用 hack insted
let route = this.router.config.find(r => r.path === '/erro');
route.data = {name: 'name' };
this.router.navigateByUrl('/error')
or或者
user queryParam to pass Data用户 queryParam 传递数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.