angular 5+相同的路由,相同的组件,但参数不同,在angular subscription上不发送http get请求

时间:2018-12-09 12:33:41

标签: angular http routing httpclient

这是我的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>

// Thid是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与我的问题类似,但不完全相同。但是,我尝试了但失败了。预先感谢

2 个答案:

答案 0 :(得分: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);
 });
 ) 

答案 1 :(得分:0)

直接用// Get all term Ids values for the current product attribute (array) $term_ids = get_terms(array('taxonomy' => $taxonomy, 'fields' => 'ids', 'hide_empty' => false)); 来获取参数怎么样:

pipe