首页 > 解决方案 > 无法解决超过最大调用堆栈大小

问题描述

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);
  }

谁能告诉我我在这里做错了什么?

标签: angulartypescript

解决方案


将 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,
},

推荐阅读