javascript - 错误类型错误: this.route.params.flatMap不是函数

标签 javascript angular

我只是想转换到 id。嗯,或者类似的东西(我只是不知道如何解释)。一般来说,当你按下一个按钮去特定用户时,会出现这样的错误:

ERROR TypeError: this.route.params.flatMap is not a function

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

at checkAndUpdateDirectiveInline (core.js:12369)

at checkAndUpdateNodeInline (core.js:13893)

at checkAndUpdateNode (core.js:13836)

at debugCheckAndUpdateNode (core.js:14729)

at debugCheckDirectivesFn (core.js:14670)

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

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

at checkAndUpdateView (core.js:13802)

at 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());
  }

}

最佳答案

只需使用 mergeMap 代替 flatMap

import 'rxjs/add/operator/mergeMap';

看看rxjs flatmap missing .

关于javascript - 错误类型错误: this.route.params.flatMap不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50832143/

相关文章:

javascript - Chrome 扩展消息传递

javascript - ngSwitchCase 没有提供者

javascript - 切换按钮 - 显示/隐藏信息

Angular 2 单例服务不适合我

angular - 如何在选项 Angular 中获取所选项目

javascript - 光标悬停时播放的音乐

javascript - react native : Error while downgrading from 0. 51 到 0.45

javascript - Google Drive API,无法通过 JS 打开标准共享对话框(x-frame-options 错误)

javascript - 如何隐藏/显示基于其他选项集的选项集 CRM Dynamics

angular - 如何使用 Cypress 测试 AWS Amplify Angular Authenticator 组件?