0

I'm using PhpStorm to develop an Angular app and I have an "unresolved variable" error at

params.myid

Here is the code line:

 this.route.params.subscribe((params: Params) => {
          const avalancheid = params.myid;
          });

There is no error if I change the type to "any" instead of "Params"

 this.route.params.subscribe((params: any) => {
          const avalancheid = params.myid;
          });

Angular version: 4.2.4, PhpStorm version: 2017.2.4

2 Answers 2

2

That's because your Params class doesn't contain the key myid, by using square brackets params['myid'] you should be able to get the value as shown in the docs.

The reason it's crashing is because you're telling the TypeScript compiler "Hey, I'm expecting Params", at which point you then proceed to tell it to get a property named myid which the original Params class doesn't contain, hence the error.

Sign up to request clarification or add additional context in comments.

1 Comment

I usually don't comment on someone's answer, so sorry for sounding rude, but you can use the dot notation to navigate a Type like Params, because it's not a Class, it's a Type. And when you try to link to the 'docs', please use the official docs. Here you can see an example using the params dot. The one you are referring to, is still on angular2, even though version 5 is out. :)
1

There can be two issues here.

  1. You are not importing Params from @angular/router:

like this:

import {Params} from "@angular/router";

You need this Params type, because it is defined as [key: string]: any, which allows for using the dot notation to navigate through the object.

  1. Wrong typescript version:

This 'allowing' for using the dot nation is a relatively new feature though of TypeScript. So the other problem could be is that you are using an older version of TypeScript. Try updating to at least version 2.4.x, to be able to use this feature, and while you are at it. See if you can update to the latest angular version as well. It's worth it.

If you are not willing to update, even though I strongly encourage you to do so, you can use the other notation to access the property:

this.route.params.subscribe((params: Params) => {
  const avalancheid = params['myid'];
});

1 Comment

Thanks for fast answer! Sorry i forgot to write that Params was imported and I will update typescript!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.