¿Combinando parámetros de ruta angular?

Oct 07 2020

La aplicación Angular Material Docs combina parámetros de ruta como este:

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

He aquí un ejemplo: https://github.com/angular/material.angular.io/blob/master/src/app/pages/component-category-list/component-category-list.ts

Sin embargo, esto produce el error:

@deprecated - resultSelector ya no es compatible, canalizar al mapa en su lugar

Entonces, para evitarlo, es posible hacer esto:

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

Como se documenta aquí: combineLatest refactoring para @deprecated - resultSelector ya no es compatible, ¿canalizar al mapa en su lugar?

Sin embargo, eso produce este error:

ERROR TypeError: No se puede leer la propiedad 'nombre' de indefinido en SafeSubscriber._next (component-category-list.ts: 50) en SafeSubscriber .__ tryOrUnsub (Subscriber.js: 183) en SafeSubscriber.next (Subscriber.js: 122) en Subscriber ._next (Subscriber.js: 72) en Subscriber.next (Subscriber.js: 49) en MapSubscriber._next (map.js: 35) en MapSubscriber.next (Subscriber.js: 49) en CombineLatestSubscriber.notifyNext (combineLatest.js : 73) en InnerSubscriber. _trySubscribe (Subject.js: 81) en BehaviorSubject.subscribe (Observable.js: 28) en subscribeToResult (subscribeToResult.js: 9)

Por lo tanto, parece que el sectionparámetro de URL que está buscando el componente ya no se captura después de la refactorización. Pensamientos

Respuestas

Ole Oct 09 2020 at 11:23

Los parámetros deben desestructurarse al pasar Object.assignasí:


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