Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

Angular activatedRoute paramMap.params not known by TypeScript

I'm subscribing to this.activatedRoute.paramMap which according to TS is of type ParamMap. This is all nice but when I'm debugging it seems to have this convenient property paramMap.params which I can use like this

.subscribe((paramMap: any) => {
  const { projectId, domainId, chapterId, unitId } = paramMap.params;
});

However, this params property is not known to exist on ParamMap according to TS so I need to cast it.
What is going on here? Am I doing this wrong?

enter image description here

In DevTools I'm seeing "ParamsAsMap" but this is not a known type I can cast to. I'm not even sure where this is coming from, Google search turns blank, where does DevTools even get this from?

question from:https://stackoverflow.com/questions/65905208/angular-activatedroute-parammap-params-not-known-by-typescript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

params property of ParamsMap is defined here

Like @jonrsharpe mentioned, it is defined on the object itself,

.subscribe((paramMap) => {
     console.log(paramMap.[<yourQueryParamKey>])
});

The type of paramMap is Params, So actually decoupling the type is: .subscribe((paramMap: [key: string]: any) => ...

[key: string]: any correlates with everything (including params property) so this is actually typed


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...