错误TypeError:this.route.params.flatMap不是函数

时间:2018-06-13 08:08:25

标签: javascript angular

我只想过渡到id。嗯,或类似的东西(我只是不知道如何解释它)。通常,当您按某个按钮转到特定用户时,会出现这样的错误:

  

ERROR TypeError:this.route.params.flatMap不是函数

     

at UserShowComponent.ngOnInit(user-show.component.ts:31)

     

在checkAndUpdateDirectiveInline(core.js:12369)

     

在checkAndUpdateNodeInline(core.js:13893)

     

在checkAndUpdateNode(core.js:13836)

     

在debugCheckAndUpdateNode(core.js:14729)

     

在debugCheckDirectivesFn(core.js:14670)

     

at Object.eval [as updateDirectives](UserShowComponent_Host.ngfactory.js?[sm]:1)

     

at Object.debugUpdateDirectives [as updateDirectives](core.js:14655)

     

在checkAndUpdateView(core.js:13802)

     

在callViewAction(core.js:14153)

如何解决这个问题?

import { Component, OnInit, Input } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { Angular2TokenService } from "angular2-token";
import { UserService} from '../../../../services/user/user.service';
import { User } from '../../../../models/user.model';

@Component({
  selector: 'app-user-show',
  templateUrl: './user-show.component.html',
  styleUrls: ['./user-show.component.css']
})
export class UserShowComponent implements OnInit {

  id: number;
  routeId: any;

  constructor(
    private authTokenService: Angular2TokenService, 
    private route: ActivatedRoute,
    private servUser: UserService,
  ) { }

  @Input() user: User;

  ngOnInit() {
    this.routeId = this.route.params.subscribe(
      params => {
        this.id = +params['id'];
      }
    )
    let userRequest = this.route.params.flatMap((params: Params) => 
        this.servUser.getUser(+params['id']));
    userRequest.subscribe(response => this.user = response.json());
  }

}

1 个答案:

答案 0 :(得分:0)

只需使用mergeMap

flatMap内容即可
import 'rxjs/add/operator/mergeMap';

看看rxjs flatmap missing