angular 5+相同的路由,相同的组件但不同的param,不在角度订阅上发送http get请求

问题描述 投票:0回答:2

这是我的app.modules

const appRoutes: Routes = [
  { path: 'topic/:topicTypeAngular', component: StoryTypeComponent, pathMatch : 'full'},
  { path: '**', component: PageNotFoundComponent}
];

这是我在html中的导航

<ul>
    <li> <a routerLink="topic/culture">Culture</a></li>
    <li><a routerLink="topic/tech">Tech</a></li>
</ul>

//这是component.ts摘录和关键部分

import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpRequest, HttpEvent, HttpEventType, HttpParams } from '@angular/common/http';
import { ActivatedRoute } from '@angular/router';

   constructor(public http: HttpClient, private route: ActivatedRoute) {
         this.parameter = this.route.snapshot.paramMap.get('topicTypeAngular');
         let parameter = this.parameter;
    }


    ngOnInit() {
         this.route.url.subscribe(parameter => {
         let parameter = this.parameter;
          this.http.get('http://localhost:3200/cookbooks/topic/'+parameter).subscribe(httpResponse => {
              this.data = httpResponse;
              console.log(httpResponse);
            });

        })
     }

最后一部分没有提供新的httpResponse数据。这个link类似于我的问题,但不完全相同。但是,我试过但失败了。提前致谢

angular http routing httpclient
2个回答
1
投票

试试这个:

    constructor(public http: HttpClient, private route: ActivatedRoute) { }

ngOnInit() {

this.route.params.subscribe( param =>  this.http.get('http://localhost:3200/something/topic/'+param['topicTypeAngular']).subscribe(httpResponse => 
{ this.data = httpResponse; console.log(httpResponse);
 });
 ) 

0
投票

怎么样用这样的pipe直接获得params:

constructor(public http: HttpClient, private route: ActivatedRoute) {    
  this.route.paramMap.pipe(
    switchMap((params: ParamMap) => {
      this.parameter = params.get('topicTypeAngular');
      return this.http.get(`http://localhost:3200/something/topic/${this.parameter}`);
    })
  ).subscribe(
    result => console.log(result)
  );
}
© www.soinside.com 2019 - 2024. All rights reserved.