Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 类型';{}';不可分配给类型';字符串';_Typescript_Angular_Router_Rxjs5_Ngrx - Fatal编程技术网

Typescript 类型';{}';不可分配给类型';字符串';

Typescript 类型';{}';不可分配给类型';字符串';,typescript,angular,router,rxjs5,ngrx,Typescript,Angular,Router,Rxjs5,Ngrx,我正在使用ngrx/路由器 当我打开http://localhost/set-new-password/abc,路线图运行良好。我可以得到我想要的字符串token const landingRouter: Routes = [ { path: '/set-new-password/:token', component: SetNewPasswordComponent }, ]; export class SetNewPasswordComponent implements OnInit {

我正在使用ngrx/路由器

当我打开
http://localhost/set-new-password/abc
路线图
运行良好。我可以得到我想要的字符串
token

const landingRouter: Routes = [
  { path: '/set-new-password/:token', component: SetNewPasswordComponent },
];

export class SetNewPasswordComponent implements OnInit {
  token: string = '';

  constructor(private _routeParams$: RouteParams) {}

  ngOnInit()
  {
    this._routeParams$
      .pluck('token')
      .subscribe(token => {
        console.log(typeof token); // Console: "string"
        console.log(token);        // Console: "abc"
        this.token = token;        // Terminal: Type '{}' is not assignable to type 'string'.
      });
  }
}
但是,我在我的终端上收到了以下警告:

类型“{}”不可分配给类型“string”

我知道我可以使用
this.token=String(token)以摆脱它


但这一警告为何会显现出来?有人能给我解释一下吗?谢谢

从@brandonroberts获得@MikeRyan52原始答案的帮助,谢谢

这不起作用的两个原因:

  • 直到运行时,我们才知道params对象的形状, 所以没有一个好的类型来描述它

  • 由于使用了字符串选择器,因此无法正确键入Pull('id') 谁也不知道

  • 解决方案是显式键入pull():

    params$.pluck('id'))
    
    因此,我的情况正在转变为:

    this._routeParams$
      .pluck<string>('token')
      .subscribe(token => this.token = token);
    
    this.\u路线图$
    .Pulk(‘代币’)
    .subscribe(token=>this.token=token);
    
    this._routeParams$
      .pluck<string>('token')
      .subscribe(token => this.token = token);