Typescript 类型';{}';不可分配给类型';字符串';
我正在使用ngrx/路由器 当我打开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 {
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);