要解决Angular导航路由在Observable中无法正常工作的问题,可以使用以下代码示例中的解决方法:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Subject } from 'rxjs';
@Component({
selector: 'app-navigation',
templateUrl: './navigation.component.html',
styleUrls: ['./navigation.component.css']
})
export class NavigationComponent implements OnInit {
private navigateSubject: Subject = new Subject();
constructor(private router: Router) { }
ngOnInit() {
this.navigateSubject.subscribe((route: string) => {
this.router.navigate([route]);
});
}
navigateTo(route: string) {
this.navigateSubject.next(route);
}
}
navigateTo
方法来触发导航事件:Home
About
通过上述解决方法,当用户点击导航链接或按钮时,会触发导航事件并通过路由导航到指定的页面。使用Observable(Subject)来处理导航事件,确保在组件中正确地进行路由导航。
上一篇:Angular导航路由
下一篇:Angular导航提示