how to reload app.components ionic 2
why would you do that?
I need to reload my app.components to show the side menu as âLogoutâ after logging in, which was before âLoginâ
add import { App } from 'ionic-angular';
constructor public appCtrl: App
and
this.appCtrl.getRootNav().setRoot(yourcomponents);
Thatâs not really a good way to achieve thatâŚ
try it
this.appCtrl.getRootNav().setRoot(yourcomponent);
window.location.reload()
I want to do this since in the sidemenu, I am displaying data from the logged in user, I store the data in native storage, and as the menus are in component.ts to see the changes of the login I have to close the ap and reopen it,
How can I make changes to the menu reflect just logon?
So does anyone know how this is supposed to be done? I take it youâve been answered @bengtler & @rlouie
You could probably trigger the update of something using Events: http://ionicframework.com/docs/api/util/Events/
IMHO the entire premise of this discussion is flawed. âReloadingâ is an imperative concept, and when you try to think imperatively you are fighting against the framework, which wants you to think reactively. So,
Itâs not. You donât want to be âforcingâ or âreloadingâ anything. React to whatever change in circumstances has happened. For a specific example of managing user authentication status, see here.
There is one scenario where this would be extremely useful⌠effectively a way to manually call a components ngOnInit() method. Consider this scenario:
- Iâm at the root page of my app, which has tabs
- I navigate to a screen from the first tab
- Before navigating I lose connection to the internet
- I show a âNot connectedâ message and retry until connection is restored
- Upon restoring the connection, i want to refresh the screen that I was navigating to
In order to do this in a clean modular way, I would like to know what the âcurrent viewâ is, and then ârefreshâ or âre-initâ that view. In the reactive way, I would have to subscribe to a âreconnectedâ event for every view in my app, which I definitely donât want to do.
Any thoughts on a better way to do this?
+1 I need to do something very similar
But the documentation of the new beta donât include the events page, and if I try to use:
import { Events } from 'ionic-angular';
I have an error
How is the new way to achieve it?
Thanks a lot, but I finally find how to import it:
import { Events } from '@ionic/angular';
import { Events } from âionic-angularâ;
// first page (publish an event when a user is created)
constructor(public events: Events) {}
createUser(user) {
console.log(âUser created!â)
this.events.publish(âuser:createdâ, user, Date.now());
}
// second page (listen for the user created event after function is called)
constructor(public events: Events) {
events.subscribe(âuser:createdâ, (user, time) => {
// user and time are the same arguments passed in events.publish(user, time)
console.log(âWelcomeâ, user, âatâ, time);
});
}
This is working successfully. I used this to update a side menu that shows different items when user is logged in or not.
Everytime the user logs in or out, menu refreshing method is fired in my app component through the event.
Thank you!