【问题标题】:Route action does not work on a component and different parameters路由操作不适用于组件和不同的参数
【发布时间】:2017-10-26 16:43:23
【问题描述】:

路由操作不适用于组件和不同的参数。

我有一个获取不同参数的组件,如下所示。但是,当进入组件路由时,它只加载一次并且...

this.route.snapshot.params['id'] 未更新。

路线

export const ROUTES: Routes = [
    {path: '', component: AppComponent,
        children: [
            { path: '', redirectTo: 'home', pathMatch: 'full'},
            { path: 'home', component: HomeComponent },
            { path: 'categoria/:id', component: ListaComponent },
            { path: 'player/:id', component: PlayerComponent }]
    }]

组件

import {Component, OnInit } from '@angular/core';
import { PostServices } from '../postServices';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'net-lista',
  templateUrl: './lista.component.html',
})
export class ListaComponent implements OnInit {

    order: any;

  constructor(private postServices: PostServices, private route: ActivatedRoute) {

  }

  ngOnInit() {
      this.postServices.getPostByCategory(this.route.snapshot.params['id']).subscribe( (res) => {
          this.order = res;
      })
  }

}

【问题讨论】:

    标签: angular angular-router


    【解决方案1】:

    快照值只会为您提供参数的第一个值 , 而是订阅 this.route.params Observable 并且每次路由中的参数 id 更改 Observable 都会解析:

     ngOnInit() {
              this.route.params.subscribe((params)=>{
              this.postServices.getPostByCategory(params["id"]).subscribe( (res) => {
                  this.order = res;
               })
           })
      }
    

    【讨论】:

    • 嗨。 'Observable 类型不存在属性 'subsribe'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    相关资源
    最近更新 更多