Angular 角度,在rooter导航后显示成功警报
我试图找到一种方法,在我成功填写表格并重定向到另一个页面后,弹出消息或发出警报 导航后类似于“新表单已成功添加” 这是我的rooter导航代码Angular 角度,在rooter导航后显示成功警报,angular,typescript,bootstrap-4,Angular,Typescript,Bootstrap 4,我试图找到一种方法,在我成功填写表格并重定向到另一个页面后,弹出消息或发出警报 导航后类似于“新表单已成功添加” 这是我的rooter导航代码 specialEvents = [] constructor( private _eventService: EventService, private _router: Router) { } ngOnInit(): void { this._eventService.getSpecialEvents()
specialEvents = []
constructor( private _eventService: EventService,
private _router: Router) { }
ngOnInit(): void {
this._eventService.getSpecialEvents()
.subscribe(
res => this.specialEvents = res,
err => {
if( err instanceof HttpErrorResponse ) {
if (err.status === 401) {
this._router.navigate(['/home'])
}
}
}
)
}
任何人都可以告诉我如何在之后显示弹出窗口或成功警报。_router.navigate(['/home'])是的,您可以设置超时:
ngOnInit(): void {
this._eventService.getSpecialEvents()
.subscribe(
res => this.specialEvents = res,
err => {
if( err instanceof HttpErrorResponse ) {
if (err.status === 401) {
this._router.navigate(['/home'])
setTimeout(() => {
window.alert('Unauthorized');
}, 500);
}
}
}
)
}
您需要在链接到路由“/home”的组件的ngOnInit方法中添加警报弹出事件 例如: 如果HomeComponent链接到应用程序路由器中的路径“/home”,则 {路径:'home',组件:HomeComponent}
然后,在路由器导航到“/主页”期间,将启动组件“HomeComponent”。因此,在“HomeComponent”的ngOnInit方法中,添加警报弹出功能以显示警报消息。您有很多选择
导航之后,您可以使用乐观的方法来呼叫您的toast或alert:
应用程序组件中
可以添加以下内容:
this.router.events.pipe(filter(event => event instanceof NavigationEnd)).subscribe((event: NavigationEnd) => {
if (event.url.inlcudes('home')) alert();
});
ngAfterViewInit() {
setTimeout(() => {
if (this.router.url === '/home') {
window.alert('welcome');
}
},1000)
}
谢谢,这就是我想要的
this.router.navigate(['/', 'home'])
.then(nav => {
window.alert('Home');
}, err => {
console.log(err) // when there's an error
});
ngAfterViewInit() {
setTimeout(() => {
if (this.router.url === '/home') {
window.alert('welcome');
}
},1000)
}