0

I have the following structure:

Structure

In the app.component.ts I have a variable "valor", I want to use that variable in app-routing.module.ts

app.component.ts

app-routing.module.ts

Is that possible?

3 Answers 3

1

As Sid T pointed it, you could use Nav Parameters. For instance, you would navigate like this:

app.component.ts

goToThisPage(variable) {
    this.navCtrl.push(TargetPage, {
      param: variable
    });
}

and would retrieve the variable like this:

app-routing.module.ts

this.xyz = this.navParams.get('param');
Sign up to request clarification or add additional context in comments.

1 Comment

this question relates to Ionic 4 and I guess it's better to leverage something else than NavController that only supports non-lazy components nowadays. Also Unclear how your answer can help in this specific use case as there are no "pages" really.
1

The best way to use a common variable in any of page is to use provider. First generate provider in your project using following command :

ionic generate provider provider_name

and declare that common variable as a public in this provider like :

public test:string = "";

Now, import and inject this provider in any of page that you want to use that common variable as below :

import {provider_name} from '../..';
constructor(private testPvdr:TestProvider) {}

Now, you can use that common variable by just provider :

alert(this.testPvdr.test);

Comments

0

You can use Nav Params to transfer data from one page to another.

Comments

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.