correct way to init first view in app

20 views
Skip to first unread message

Alexander Mills

unread,
Dec 22, 2020, 7:34:18 AM12/22/20
to Angular and AngularJS discussion
when the page first loads I want to navigate to /login or /signup depending on localStorage value. but I don't want to render anything before navigating to one of those. I have this in app.component.ts:

import { Component } from '@angular/core';
import {Router} from "@angular/router";

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {

constructor(
private router: Router
) {}

getStoredEmail(){
return localStorage.getItem('email')
}

ngOnInit(): void{
if(this.getStoredEmail()){
this.router.navigate(['/login'])
} else {
this.router.navigate(['/signup'])
}
}
}

is this the right approach? the problem is that it will render the app component before rendering the login or signup component?

Arnaud Deman

unread,
Dec 23, 2020, 3:58:34 AM12/23/20
to Angular and AngularJS discussion
Hello,

Regards,
Arnaud.

Alexander Mills

unread,
Dec 23, 2020, 4:34:51 AM12/23/20
to ang...@googlegroups.com
Hi Arnaud -
what I mean is: the app.component will render some default view (say home), then switch to signup or login. what I want is to not render home, just something blank and then switch to signup or login? not sure if that makes sense. basically dont want to show/render something for 50ms, then switch to something else.

--
You received this message because you are subscribed to a topic in the Google Groups "Angular and AngularJS discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/HvwXNfqzk2c/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/angular/648e70b9-52cb-42a2-a27e-b77f99c81da3n%40googlegroups.com.


--
Alexander D. Mills
New cell phone # (415)730-1805
linkedin.com/in/alexanderdmills

Arnaud Deman

unread,
Dec 23, 2020, 4:55:31 AM12/23/20
to Angular and AngularJS discussion
The guard would be executed before the component is rendered so you could have your test in it and use the router to navigate to login or signup.
Regards,
Arnaud.

Alexander Mills

unread,
Dec 23, 2020, 5:37:38 AM12/23/20
to ang...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages