【问题标题】:Combining Angular Path Parameters?结合角度路径参数?
【发布时间】:2021-01-22 20:27:37
【问题描述】:

Angular Material Docs 应用程序组合路径参数如下:

    // Combine params from all of the path into a single object.
    this.params = combineLatest(
        this._route.pathFromRoot.map(route => route.params), Object.assign);

这是一个例子: https://github.com/angular/material.angular.io/blob/master/src/app/pages/component-category-list/component-category-list.ts

但是这会产生错误:

@deprecated — 不再支持 resultSelector,改为通过管道映射

为了解决这个问题,可以这样做:

    this.params = combineLatest(
      this._route.pathFromRoot.map(route => route.params)
    ).pipe(
        map(Object.assign)
    );

如此处所述: combineLatest refactoring for @deprecated — resultSelector no longer supported, pipe to map instead?

然而这会产生这个错误:

错误类型错误:无法读取未定义的属性“名称” 在 SafeSubscriber._next (component-category-list.ts:50) 在 SafeSubscriber.__tryOrUnsub (Subscriber.js:183) 在 SafeSubscriber.next (Subscriber.js:122) 在 Subscriber._next (Subscriber.js:72) 在 Subscriber.next (Subscriber.js:49) 在 MapSubscriber._next (map.js:35) 在 MapSubscriber.next (Subscriber.js:49) 在 CombineLatestSubscriber.notifyNext (combineLatest.js:73) 在 InnerSubscriber._next (InnerSubscriber.js:11) 在 InnerSubscriber.next (Subscriber.js:49) 在 BehaviorSubject._subscribe (BehaviorSubject.js:14) 在 BehaviorSubject._trySubscribe (Observable.js:42) 在 BehaviorSubject._trySubscribe (Subject.js:81) 在 BehaviorSubject.subscribe (Observable.js:28) 在 subscribeToResult (subscribeToResult.js:9)

所以看起来组件正在寻找的section URL 参数在重构后不再被捕获。 想法?

【问题讨论】:

    标签: javascript angular typescript rxjs


    【解决方案1】:

    传递给Object.assign时必须像这样解构参数:

    
        // Combine params from all of the path into a single object.
        this.params = combineLatest(
          this._route.pathFromRoot.map(route => route.params)).
          pipe(map((params:Params[])=>Object.assign({}, ...params)));
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-28
      • 2018-05-17
      • 2013-08-28
      • 1970-01-01
      相关资源
      最近更新 更多