Angular 管线操作不适用于组件和不同的参数
管线操作不适用于组件和不同的参数 我有一个获得不同参数的组件,如下所示。但是,当进入组件管线时,它只加载一次,并且 此.route.snapshot.params['id']未更新 路线Angular 管线操作不适用于组件和不同的参数,angular,angular-router,Angular,Angular Router,管线操作不适用于组件和不同的参数 我有一个获得不同参数的组件,如下所示。但是,当进入组件管线时,它只加载一次,并且 此.route.snapshot.params['id']未更新 路线 export const ROUTES: Routes = [ {path: '', component: AppComponent, children: [ { path: '', redirectTo: 'home', pathMatch: 'full'},
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;
})
}
}
快照值将只提供参数的第一个值 ,改为订阅this.route.params Observable,每次路由中的参数id更改时,Observable将解析:
ngOnInit() {
this.route.params.subscribe((params)=>{
this.postServices.getPostByCategory(params["id"]).subscribe( (res) => {
this.order = res;
})
})
}
你好“Observable”类型上不存在属性“subscribe”