How to change the default opening page to a component other than AppComponent?
up vote
0
down vote
favorite
I have an Angular 6 application. At first it was just one page (let's say "search page"), so I put the page content in
app.component.html and related functions, etc. to the app.component.ts. So the search page is in appcomponent.
Now I was asked to add another page (Login page). So, now the first page that needs to be opened when
the page is first loaded is login page. If the login is successful, then "search page" should open.
I am new to Angular so I look at some routing & navigation tutorials and applied what they are doing.
The problem is that now when the page is first loaded it opens both LoginComponent and AppComponent (search page) together.
At the top of the page login screen and under that search screen. Also, if I go to "/search" then this time
there is two search pages one under the other.
So I have one extra search page.I get that the problem is that the search page is being in appcomponent.
I guess that the app loads this component by default. What I want is that I want to display LoginComponent
first (when the page is first loaded), if the login is successful, navigate to search page.
Here is what I have done to add routing:
I added
import {RouterModule} from '@angular/router';to my app.module.ts
Also I added RouterModule in imports like this (again in app.module.ts)
imports: [
RouterModule.forRoot([
{path: '', component: LoginComponent},
{path: 'search', component:AppComponent}
])
],
Added
<router-outlet></router-outlet>in app.component.html. This HTML file included my search page content like navbar, table etc. And at the beginning of it I put router line (I know it is wrong to put inside my search page but In the tutorial I watched, he put<router-outlet></router-outlet>in app.component.html. I guess that the reason for my double pages is this HTML file but could not get it to work somewhere else.)In login.component.html,
Login page content and of course a Login button with (click)="onLogin()"login.component.ts includes onLogin function if the login is successful
this.router.navigate(['/search']);Finally in my index.html I call the appcomponent inside body
<app-root></app-root>
Right now navigation with login works but as I said the initial page with URL: "http://localhost:4200/"
includes both login and search page. Also URL: "http://localhost:4200/search" includes two search pages.
angular angular6
add a comment |
up vote
0
down vote
favorite
I have an Angular 6 application. At first it was just one page (let's say "search page"), so I put the page content in
app.component.html and related functions, etc. to the app.component.ts. So the search page is in appcomponent.
Now I was asked to add another page (Login page). So, now the first page that needs to be opened when
the page is first loaded is login page. If the login is successful, then "search page" should open.
I am new to Angular so I look at some routing & navigation tutorials and applied what they are doing.
The problem is that now when the page is first loaded it opens both LoginComponent and AppComponent (search page) together.
At the top of the page login screen and under that search screen. Also, if I go to "/search" then this time
there is two search pages one under the other.
So I have one extra search page.I get that the problem is that the search page is being in appcomponent.
I guess that the app loads this component by default. What I want is that I want to display LoginComponent
first (when the page is first loaded), if the login is successful, navigate to search page.
Here is what I have done to add routing:
I added
import {RouterModule} from '@angular/router';to my app.module.ts
Also I added RouterModule in imports like this (again in app.module.ts)
imports: [
RouterModule.forRoot([
{path: '', component: LoginComponent},
{path: 'search', component:AppComponent}
])
],
Added
<router-outlet></router-outlet>in app.component.html. This HTML file included my search page content like navbar, table etc. And at the beginning of it I put router line (I know it is wrong to put inside my search page but In the tutorial I watched, he put<router-outlet></router-outlet>in app.component.html. I guess that the reason for my double pages is this HTML file but could not get it to work somewhere else.)In login.component.html,
Login page content and of course a Login button with (click)="onLogin()"login.component.ts includes onLogin function if the login is successful
this.router.navigate(['/search']);Finally in my index.html I call the appcomponent inside body
<app-root></app-root>
Right now navigation with login works but as I said the initial page with URL: "http://localhost:4200/"
includes both login and search page. Also URL: "http://localhost:4200/search" includes two search pages.
angular angular6
therouter-outletis in your AppComponent, and you have route that redirects to AppComponent as well? what are you trying to do..?
– TheUnreal
Nov 11 at 12:20
Yes, it is a mistake. I am trying to understand/learn. Thank you for pointing that out.
– csel
Nov 11 at 12:33
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have an Angular 6 application. At first it was just one page (let's say "search page"), so I put the page content in
app.component.html and related functions, etc. to the app.component.ts. So the search page is in appcomponent.
Now I was asked to add another page (Login page). So, now the first page that needs to be opened when
the page is first loaded is login page. If the login is successful, then "search page" should open.
I am new to Angular so I look at some routing & navigation tutorials and applied what they are doing.
The problem is that now when the page is first loaded it opens both LoginComponent and AppComponent (search page) together.
At the top of the page login screen and under that search screen. Also, if I go to "/search" then this time
there is two search pages one under the other.
So I have one extra search page.I get that the problem is that the search page is being in appcomponent.
I guess that the app loads this component by default. What I want is that I want to display LoginComponent
first (when the page is first loaded), if the login is successful, navigate to search page.
Here is what I have done to add routing:
I added
import {RouterModule} from '@angular/router';to my app.module.ts
Also I added RouterModule in imports like this (again in app.module.ts)
imports: [
RouterModule.forRoot([
{path: '', component: LoginComponent},
{path: 'search', component:AppComponent}
])
],
Added
<router-outlet></router-outlet>in app.component.html. This HTML file included my search page content like navbar, table etc. And at the beginning of it I put router line (I know it is wrong to put inside my search page but In the tutorial I watched, he put<router-outlet></router-outlet>in app.component.html. I guess that the reason for my double pages is this HTML file but could not get it to work somewhere else.)In login.component.html,
Login page content and of course a Login button with (click)="onLogin()"login.component.ts includes onLogin function if the login is successful
this.router.navigate(['/search']);Finally in my index.html I call the appcomponent inside body
<app-root></app-root>
Right now navigation with login works but as I said the initial page with URL: "http://localhost:4200/"
includes both login and search page. Also URL: "http://localhost:4200/search" includes two search pages.
angular angular6
I have an Angular 6 application. At first it was just one page (let's say "search page"), so I put the page content in
app.component.html and related functions, etc. to the app.component.ts. So the search page is in appcomponent.
Now I was asked to add another page (Login page). So, now the first page that needs to be opened when
the page is first loaded is login page. If the login is successful, then "search page" should open.
I am new to Angular so I look at some routing & navigation tutorials and applied what they are doing.
The problem is that now when the page is first loaded it opens both LoginComponent and AppComponent (search page) together.
At the top of the page login screen and under that search screen. Also, if I go to "/search" then this time
there is two search pages one under the other.
So I have one extra search page.I get that the problem is that the search page is being in appcomponent.
I guess that the app loads this component by default. What I want is that I want to display LoginComponent
first (when the page is first loaded), if the login is successful, navigate to search page.
Here is what I have done to add routing:
I added
import {RouterModule} from '@angular/router';to my app.module.ts
Also I added RouterModule in imports like this (again in app.module.ts)
imports: [
RouterModule.forRoot([
{path: '', component: LoginComponent},
{path: 'search', component:AppComponent}
])
],
Added
<router-outlet></router-outlet>in app.component.html. This HTML file included my search page content like navbar, table etc. And at the beginning of it I put router line (I know it is wrong to put inside my search page but In the tutorial I watched, he put<router-outlet></router-outlet>in app.component.html. I guess that the reason for my double pages is this HTML file but could not get it to work somewhere else.)In login.component.html,
Login page content and of course a Login button with (click)="onLogin()"login.component.ts includes onLogin function if the login is successful
this.router.navigate(['/search']);Finally in my index.html I call the appcomponent inside body
<app-root></app-root>
Right now navigation with login works but as I said the initial page with URL: "http://localhost:4200/"
includes both login and search page. Also URL: "http://localhost:4200/search" includes two search pages.
angular angular6
angular angular6
edited Nov 11 at 12:28
Zoe
10.6k73575
10.6k73575
asked Nov 11 at 12:14
csel
547
547
therouter-outletis in your AppComponent, and you have route that redirects to AppComponent as well? what are you trying to do..?
– TheUnreal
Nov 11 at 12:20
Yes, it is a mistake. I am trying to understand/learn. Thank you for pointing that out.
– csel
Nov 11 at 12:33
add a comment |
therouter-outletis in your AppComponent, and you have route that redirects to AppComponent as well? what are you trying to do..?
– TheUnreal
Nov 11 at 12:20
Yes, it is a mistake. I am trying to understand/learn. Thank you for pointing that out.
– csel
Nov 11 at 12:33
the
router-outlet is in your AppComponent, and you have route that redirects to AppComponent as well? what are you trying to do..?– TheUnreal
Nov 11 at 12:20
the
router-outlet is in your AppComponent, and you have route that redirects to AppComponent as well? what are you trying to do..?– TheUnreal
Nov 11 at 12:20
Yes, it is a mistake. I am trying to understand/learn. Thank you for pointing that out.
– csel
Nov 11 at 12:33
Yes, it is a mistake. I am trying to understand/learn. Thank you for pointing that out.
– csel
Nov 11 at 12:33
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
Your approach is correct, just remove the search component from app.component html and have only login component.
You can use the Routing mechanism with authguard to navigate to search page once the login is successful
Hello, as I understand correctly, you think that I have 3 components; search, login and app. I actually have two. LoginComponent and AppComponent. The search page div's are in app.component.html. I know I should have created a separate component for search but it is too late for that :) So if I remove search content from AppComponent there will be no search page :) But if you are saying there is no other way, I will create a new component for search. Thank you!
– csel
Nov 11 at 12:30
Since the first plan was to have only search page, I put everything in AppComponent at first. But now I added LoginComponent
– csel
Nov 11 at 12:32
1
yes separating the component would give what you wanted
– Sajeetharan
Nov 11 at 12:38
I have suggested the same thing and got down votes, interesting community of stackoverflow
– Artyom Amiryan
Nov 11 at 12:46
@Artyom yes the downvote is because of that i guess. When you post duplicate answers it happens. It happened for me many times
– Sajeetharan
Nov 11 at 12:47
|
show 2 more comments
up vote
0
down vote
Just create a new component for search and remove it from app component, then change your route module to
RouterModule.forRoot([
{path: '', component: LoginComponent},
{path: 'search', component: SearchComponent}
])
And make sure that in app.component.html, only <router-outlet></router-outlet> exists
why I'm getting down votes and no one explains why?
– Artyom Amiryan
Nov 11 at 12:33
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Your approach is correct, just remove the search component from app.component html and have only login component.
You can use the Routing mechanism with authguard to navigate to search page once the login is successful
Hello, as I understand correctly, you think that I have 3 components; search, login and app. I actually have two. LoginComponent and AppComponent. The search page div's are in app.component.html. I know I should have created a separate component for search but it is too late for that :) So if I remove search content from AppComponent there will be no search page :) But if you are saying there is no other way, I will create a new component for search. Thank you!
– csel
Nov 11 at 12:30
Since the first plan was to have only search page, I put everything in AppComponent at first. But now I added LoginComponent
– csel
Nov 11 at 12:32
1
yes separating the component would give what you wanted
– Sajeetharan
Nov 11 at 12:38
I have suggested the same thing and got down votes, interesting community of stackoverflow
– Artyom Amiryan
Nov 11 at 12:46
@Artyom yes the downvote is because of that i guess. When you post duplicate answers it happens. It happened for me many times
– Sajeetharan
Nov 11 at 12:47
|
show 2 more comments
up vote
2
down vote
Your approach is correct, just remove the search component from app.component html and have only login component.
You can use the Routing mechanism with authguard to navigate to search page once the login is successful
Hello, as I understand correctly, you think that I have 3 components; search, login and app. I actually have two. LoginComponent and AppComponent. The search page div's are in app.component.html. I know I should have created a separate component for search but it is too late for that :) So if I remove search content from AppComponent there will be no search page :) But if you are saying there is no other way, I will create a new component for search. Thank you!
– csel
Nov 11 at 12:30
Since the first plan was to have only search page, I put everything in AppComponent at first. But now I added LoginComponent
– csel
Nov 11 at 12:32
1
yes separating the component would give what you wanted
– Sajeetharan
Nov 11 at 12:38
I have suggested the same thing and got down votes, interesting community of stackoverflow
– Artyom Amiryan
Nov 11 at 12:46
@Artyom yes the downvote is because of that i guess. When you post duplicate answers it happens. It happened for me many times
– Sajeetharan
Nov 11 at 12:47
|
show 2 more comments
up vote
2
down vote
up vote
2
down vote
Your approach is correct, just remove the search component from app.component html and have only login component.
You can use the Routing mechanism with authguard to navigate to search page once the login is successful
Your approach is correct, just remove the search component from app.component html and have only login component.
You can use the Routing mechanism with authguard to navigate to search page once the login is successful
answered Nov 11 at 12:20
Sajeetharan
115k27157215
115k27157215
Hello, as I understand correctly, you think that I have 3 components; search, login and app. I actually have two. LoginComponent and AppComponent. The search page div's are in app.component.html. I know I should have created a separate component for search but it is too late for that :) So if I remove search content from AppComponent there will be no search page :) But if you are saying there is no other way, I will create a new component for search. Thank you!
– csel
Nov 11 at 12:30
Since the first plan was to have only search page, I put everything in AppComponent at first. But now I added LoginComponent
– csel
Nov 11 at 12:32
1
yes separating the component would give what you wanted
– Sajeetharan
Nov 11 at 12:38
I have suggested the same thing and got down votes, interesting community of stackoverflow
– Artyom Amiryan
Nov 11 at 12:46
@Artyom yes the downvote is because of that i guess. When you post duplicate answers it happens. It happened for me many times
– Sajeetharan
Nov 11 at 12:47
|
show 2 more comments
Hello, as I understand correctly, you think that I have 3 components; search, login and app. I actually have two. LoginComponent and AppComponent. The search page div's are in app.component.html. I know I should have created a separate component for search but it is too late for that :) So if I remove search content from AppComponent there will be no search page :) But if you are saying there is no other way, I will create a new component for search. Thank you!
– csel
Nov 11 at 12:30
Since the first plan was to have only search page, I put everything in AppComponent at first. But now I added LoginComponent
– csel
Nov 11 at 12:32
1
yes separating the component would give what you wanted
– Sajeetharan
Nov 11 at 12:38
I have suggested the same thing and got down votes, interesting community of stackoverflow
– Artyom Amiryan
Nov 11 at 12:46
@Artyom yes the downvote is because of that i guess. When you post duplicate answers it happens. It happened for me many times
– Sajeetharan
Nov 11 at 12:47
Hello, as I understand correctly, you think that I have 3 components; search, login and app. I actually have two. LoginComponent and AppComponent. The search page div's are in app.component.html. I know I should have created a separate component for search but it is too late for that :) So if I remove search content from AppComponent there will be no search page :) But if you are saying there is no other way, I will create a new component for search. Thank you!
– csel
Nov 11 at 12:30
Hello, as I understand correctly, you think that I have 3 components; search, login and app. I actually have two. LoginComponent and AppComponent. The search page div's are in app.component.html. I know I should have created a separate component for search but it is too late for that :) So if I remove search content from AppComponent there will be no search page :) But if you are saying there is no other way, I will create a new component for search. Thank you!
– csel
Nov 11 at 12:30
Since the first plan was to have only search page, I put everything in AppComponent at first. But now I added LoginComponent
– csel
Nov 11 at 12:32
Since the first plan was to have only search page, I put everything in AppComponent at first. But now I added LoginComponent
– csel
Nov 11 at 12:32
1
1
yes separating the component would give what you wanted
– Sajeetharan
Nov 11 at 12:38
yes separating the component would give what you wanted
– Sajeetharan
Nov 11 at 12:38
I have suggested the same thing and got down votes, interesting community of stackoverflow
– Artyom Amiryan
Nov 11 at 12:46
I have suggested the same thing and got down votes, interesting community of stackoverflow
– Artyom Amiryan
Nov 11 at 12:46
@Artyom yes the downvote is because of that i guess. When you post duplicate answers it happens. It happened for me many times
– Sajeetharan
Nov 11 at 12:47
@Artyom yes the downvote is because of that i guess. When you post duplicate answers it happens. It happened for me many times
– Sajeetharan
Nov 11 at 12:47
|
show 2 more comments
up vote
0
down vote
Just create a new component for search and remove it from app component, then change your route module to
RouterModule.forRoot([
{path: '', component: LoginComponent},
{path: 'search', component: SearchComponent}
])
And make sure that in app.component.html, only <router-outlet></router-outlet> exists
why I'm getting down votes and no one explains why?
– Artyom Amiryan
Nov 11 at 12:33
add a comment |
up vote
0
down vote
Just create a new component for search and remove it from app component, then change your route module to
RouterModule.forRoot([
{path: '', component: LoginComponent},
{path: 'search', component: SearchComponent}
])
And make sure that in app.component.html, only <router-outlet></router-outlet> exists
why I'm getting down votes and no one explains why?
– Artyom Amiryan
Nov 11 at 12:33
add a comment |
up vote
0
down vote
up vote
0
down vote
Just create a new component for search and remove it from app component, then change your route module to
RouterModule.forRoot([
{path: '', component: LoginComponent},
{path: 'search', component: SearchComponent}
])
And make sure that in app.component.html, only <router-outlet></router-outlet> exists
Just create a new component for search and remove it from app component, then change your route module to
RouterModule.forRoot([
{path: '', component: LoginComponent},
{path: 'search', component: SearchComponent}
])
And make sure that in app.component.html, only <router-outlet></router-outlet> exists
edited Nov 11 at 12:27
Zoe
10.6k73575
10.6k73575
answered Nov 11 at 12:21
Artyom Amiryan
1,610113
1,610113
why I'm getting down votes and no one explains why?
– Artyom Amiryan
Nov 11 at 12:33
add a comment |
why I'm getting down votes and no one explains why?
– Artyom Amiryan
Nov 11 at 12:33
why I'm getting down votes and no one explains why?
– Artyom Amiryan
Nov 11 at 12:33
why I'm getting down votes and no one explains why?
– Artyom Amiryan
Nov 11 at 12:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248651%2fhow-to-change-the-default-opening-page-to-a-component-other-than-appcomponent%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
the
router-outletis in your AppComponent, and you have route that redirects to AppComponent as well? what are you trying to do..?– TheUnreal
Nov 11 at 12:20
Yes, it is a mistake. I am trying to understand/learn. Thank you for pointing that out.
– csel
Nov 11 at 12:33