angular - 无法解决超过最大调用堆栈大小
问题描述
core.js:4442 ERROR RangeError: Maximum call stack size exceeded
at SafeSubscriber.__tryOrUnsub (Subscriber.ts:271)
at SafeSubscriber.next (Subscriber.ts:207)
at Subscriber._next (Subscriber.ts:139)
at Subscriber.next (Subscriber.ts:99)
at BehaviorSubject.Subject.next (Subject.ts:70)
at BehaviorSubject.next (BehaviorSubject.ts:43)
at SharedService.push.r2F1.SharedService.setTitleInfo (shared.service.ts:92)
at AppComponent.push.Sy1n.AppComponent.nameChangeFunction (app.component.ts:190)
at app.component.ts:167
at SafeSubscriber.schedulerFn [as _next] (core.js:24931)
以上是错误。
app.component.ts 中的代码
我很困惑为什么控制台中会出现此错误?
ngAfterViewChecked() {
this.titleSubscription = this.sharedService.getTitleValue()
.subscribe(item => {
this.customNameData = false;
if (item === 'View My Request' || item === 'Request Management') {
this.nameChangeFunction(item)
}
const substring = 'customNameData';
if (item.includes(substring)) {
const length = item.length;
const res = item.substr(14, length);
this.title = res;
this.customNameData = true;
} else {
this.title = item
}
});
this.routerLinkSubscription = this.sharedService.getRouterLinkValue()
.subscribe(item => {
this.routerLink = item
});
this.cdr.detectChanges();
}
nameChangeFunction(item) {
if (item === 'View My Request' || item === 'Request Management') {
this.sharedService.setTitleInfo({
data: item
});
}
}
shared.service.ts 上的代码尽管我确实尝试清空堆栈,但在我的情况下它并没有正常工作。不确定什么是解决方案。
setTitleInfo(name: any) {
this.setTitle.next(null);
this.setTitle.next(name);
}
谁能告诉我我在这里做错了什么?
解决方案
将 Title 和 Router 导入 app.component.ts 构造函数
constructor(private router: Router,
private titleService: Title) { }
然后在 ngOnInit() 上订阅路由器事件。
this.router.events.subscribe(data => {
if (data instanceof ActivationStart) {
var title = data.snapshot.data.title;
if (title) {
this.titleService.setTitle(title);
}
else {
this.titleService.setTitle('Default Title');
}
}
})
在您的每条路线中,在数据中设置标题:
{
path: 'Path Name',
data: { title: 'Route Title' },
component: YourComponent,
},
推荐阅读
- javascript - 将 Javascript 构造函数属性传递给 mouseover 函数并仍然使用 d3.select(this)
- kotlin - ktor 与 kotlinx 序列化:如何使用 JSON.nonstrict
- android - Android 和 IOS 期望不同的资源路径 (Cordova)
- javascript - 从对象数组中提取每个对象的属性值并放入不同的数组中
- spring-boot - 没有自动配置的 Spring Boot 2 执行器
- capl - CAPL - 从 dbc 获取信号值描述
- r - 为什么我的功能有时只能工作?
- c# - 锁定内部线程不运行
- firebase - Firebase 函数对 firestore 创建事件做出反应
- c# - 使用 using 声明一个匿名元组;可能吗?