Defining relative routing in Angular
up vote
0
down vote
favorite
Update
I changed forRoot
with forChild
according to the answers.
So, basically I have to issues, let this be a submodule,
@NgModule(
imports: [
CommonModule,
ARoutingModule,
BModule
],
declarations: [AppsComponent, ...],
exports: [
AppsComponent, ...
]
)
export class AModule
and
@NgModule(
imports: [
CommonModule,
BRoutingModule
],
declarations: [AppsComponent, ...],
exports: [
AppsComponent, ...
]
)
export class BModule
So AModule
is being imported by the root module and both modules, AModule
and BModule
shall define their own routes, something like
// ./A/A-Routing.module.ts
const routes: Routes = [
path: 'A',//<-- this shall route to www.site.com/A
component: AComponent
];
@NgModule(
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
declarations:
)
export class ARoutingModule
and in SubSub I have
// ./A/B/B-Routing.module.ts
const routes: Routes = [
path: 'B', //<-- this shall route to www.site.com/A/B
component: BComponent,
]
];
@NgModule(
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
declarations:
)
export class BRoutingModule
Is this possible? When using a path printing, I can get the sub-routes, but not the subsub routes (i.e. B). And can I define the routes in B without knowing the path before that? so define B
without knowing A
www.site.com/something/else
define routes for else
without knowing something
?
Here is a stackblitz example, main
works, but the Subs
don't...
angular typescript routes
add a comment |
up vote
0
down vote
favorite
Update
I changed forRoot
with forChild
according to the answers.
So, basically I have to issues, let this be a submodule,
@NgModule(
imports: [
CommonModule,
ARoutingModule,
BModule
],
declarations: [AppsComponent, ...],
exports: [
AppsComponent, ...
]
)
export class AModule
and
@NgModule(
imports: [
CommonModule,
BRoutingModule
],
declarations: [AppsComponent, ...],
exports: [
AppsComponent, ...
]
)
export class BModule
So AModule
is being imported by the root module and both modules, AModule
and BModule
shall define their own routes, something like
// ./A/A-Routing.module.ts
const routes: Routes = [
path: 'A',//<-- this shall route to www.site.com/A
component: AComponent
];
@NgModule(
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
declarations:
)
export class ARoutingModule
and in SubSub I have
// ./A/B/B-Routing.module.ts
const routes: Routes = [
path: 'B', //<-- this shall route to www.site.com/A/B
component: BComponent,
]
];
@NgModule(
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
declarations:
)
export class BRoutingModule
Is this possible? When using a path printing, I can get the sub-routes, but not the subsub routes (i.e. B). And can I define the routes in B without knowing the path before that? so define B
without knowing A
www.site.com/something/else
define routes for else
without knowing something
?
Here is a stackblitz example, main
works, but the Subs
don't...
angular typescript routes
RouterModule.forRoot()
is used for routing only in the root module of the application. For using routes in other modules, useRouterModule.forChild()
– Sachin Gupta
Nov 10 at 16:56
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Update
I changed forRoot
with forChild
according to the answers.
So, basically I have to issues, let this be a submodule,
@NgModule(
imports: [
CommonModule,
ARoutingModule,
BModule
],
declarations: [AppsComponent, ...],
exports: [
AppsComponent, ...
]
)
export class AModule
and
@NgModule(
imports: [
CommonModule,
BRoutingModule
],
declarations: [AppsComponent, ...],
exports: [
AppsComponent, ...
]
)
export class BModule
So AModule
is being imported by the root module and both modules, AModule
and BModule
shall define their own routes, something like
// ./A/A-Routing.module.ts
const routes: Routes = [
path: 'A',//<-- this shall route to www.site.com/A
component: AComponent
];
@NgModule(
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
declarations:
)
export class ARoutingModule
and in SubSub I have
// ./A/B/B-Routing.module.ts
const routes: Routes = [
path: 'B', //<-- this shall route to www.site.com/A/B
component: BComponent,
]
];
@NgModule(
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
declarations:
)
export class BRoutingModule
Is this possible? When using a path printing, I can get the sub-routes, but not the subsub routes (i.e. B). And can I define the routes in B without knowing the path before that? so define B
without knowing A
www.site.com/something/else
define routes for else
without knowing something
?
Here is a stackblitz example, main
works, but the Subs
don't...
angular typescript routes
Update
I changed forRoot
with forChild
according to the answers.
So, basically I have to issues, let this be a submodule,
@NgModule(
imports: [
CommonModule,
ARoutingModule,
BModule
],
declarations: [AppsComponent, ...],
exports: [
AppsComponent, ...
]
)
export class AModule
and
@NgModule(
imports: [
CommonModule,
BRoutingModule
],
declarations: [AppsComponent, ...],
exports: [
AppsComponent, ...
]
)
export class BModule
So AModule
is being imported by the root module and both modules, AModule
and BModule
shall define their own routes, something like
// ./A/A-Routing.module.ts
const routes: Routes = [
path: 'A',//<-- this shall route to www.site.com/A
component: AComponent
];
@NgModule(
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
declarations:
)
export class ARoutingModule
and in SubSub I have
// ./A/B/B-Routing.module.ts
const routes: Routes = [
path: 'B', //<-- this shall route to www.site.com/A/B
component: BComponent,
]
];
@NgModule(
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
declarations:
)
export class BRoutingModule
Is this possible? When using a path printing, I can get the sub-routes, but not the subsub routes (i.e. B). And can I define the routes in B without knowing the path before that? so define B
without knowing A
www.site.com/something/else
define routes for else
without knowing something
?
Here is a stackblitz example, main
works, but the Subs
don't...
angular typescript routes
angular typescript routes
edited Nov 10 at 17:52
asked Nov 10 at 16:26
rst
1,536728
1,536728
RouterModule.forRoot()
is used for routing only in the root module of the application. For using routes in other modules, useRouterModule.forChild()
– Sachin Gupta
Nov 10 at 16:56
add a comment |
RouterModule.forRoot()
is used for routing only in the root module of the application. For using routes in other modules, useRouterModule.forChild()
– Sachin Gupta
Nov 10 at 16:56
RouterModule.forRoot()
is used for routing only in the root module of the application. For using routes in other modules, use RouterModule.forChild()
– Sachin Gupta
Nov 10 at 16:56
RouterModule.forRoot()
is used for routing only in the root module of the application. For using routes in other modules, use RouterModule.forChild()
– Sachin Gupta
Nov 10 at 16:56
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
There are a few things that you'll have to fix to make it work properly.
- You'll have to add an opening route for your
SubModule
which will be a Lazy Loaded Module. You'll do this in your main-routing.module.ts file
const routes: Routes = [
path: 'main',
component: MainComponent,
children: [
path: 'sub',
loadChildren: './sub/sub.module#SubModule'
]
];
- You'll then create a module named
SubModule
and theSubRoutingModule
will act as a routing module for thisSubModule
. You'll declare your Sub1Component and Sub2Component here:
import Sub1Component, Sub2Component from './sub.component';
import NgModule from '@angular/core';
import Routes, RouterModule from '@angular/router';
import SubRoutingModule from './sub-routing.module';
@NgModule(
declarations: [Sub1Component, Sub2Component],
imports: [SubRoutingModule]
)
export class SubModule
So now your routes will change to :
<a routerLink="/main/sub/sub1">Sub1</a> |
<a routerLink="/main/sub/sub2">Sub2</a>
Here's an Updated StackBlitz for your ref.
I need to have multiple routing files at different locations.
– rst
Nov 10 at 17:18
There's a serious architectural issue with your Current Implementation. Can you please share a more concrete example of your modules and probably a minimal stackblitz sample replicating your issue?
– SiddAjmera
Nov 10 at 17:19
Stackblitz example in the op.
– rst
Nov 10 at 17:53
@rst, please go through my updated answer. Hopefully this should help you achieve what you're trying to.
– SiddAjmera
Nov 10 at 18:51
add a comment |
up vote
1
down vote
Change forRoot
to forChild
in SubSubRoutingModule
// ./Sub/subsub/SubSub-Routing.module.ts
const routes: Routes = [
path: 'subsub', //<-- this shall route to www.site.com/sub/subsub
component: SubSubComponent,
]
];
@NgModule(
imports: [RouterModule.forChild(routes)], //<-- change here
exports: [RouterModule],
declarations:
)
export class SubSubRoutingModule
That works, butSubSub
(I changed it in the OP to A and B, thought that was more clearer) get routet to the root, i.e./subsub
instead of/Sub/subsub
– rst
Nov 10 at 17:15
You should implement with Lazy Module. Look at this demo for reference stackblitz.com/edit/angular-hcerdo
– Sunil Singh
Nov 10 at 17:46
I have added a stackblitz example
– rst
Nov 10 at 17:52
1
Here is the modified version of your demo stackblitz.com/edit/angular-3mjttd
– Sunil Singh
Nov 10 at 18:04
That's exactly what I was Looking for! Excellent
– rst
Nov 10 at 19:16
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
There are a few things that you'll have to fix to make it work properly.
- You'll have to add an opening route for your
SubModule
which will be a Lazy Loaded Module. You'll do this in your main-routing.module.ts file
const routes: Routes = [
path: 'main',
component: MainComponent,
children: [
path: 'sub',
loadChildren: './sub/sub.module#SubModule'
]
];
- You'll then create a module named
SubModule
and theSubRoutingModule
will act as a routing module for thisSubModule
. You'll declare your Sub1Component and Sub2Component here:
import Sub1Component, Sub2Component from './sub.component';
import NgModule from '@angular/core';
import Routes, RouterModule from '@angular/router';
import SubRoutingModule from './sub-routing.module';
@NgModule(
declarations: [Sub1Component, Sub2Component],
imports: [SubRoutingModule]
)
export class SubModule
So now your routes will change to :
<a routerLink="/main/sub/sub1">Sub1</a> |
<a routerLink="/main/sub/sub2">Sub2</a>
Here's an Updated StackBlitz for your ref.
I need to have multiple routing files at different locations.
– rst
Nov 10 at 17:18
There's a serious architectural issue with your Current Implementation. Can you please share a more concrete example of your modules and probably a minimal stackblitz sample replicating your issue?
– SiddAjmera
Nov 10 at 17:19
Stackblitz example in the op.
– rst
Nov 10 at 17:53
@rst, please go through my updated answer. Hopefully this should help you achieve what you're trying to.
– SiddAjmera
Nov 10 at 18:51
add a comment |
up vote
1
down vote
accepted
There are a few things that you'll have to fix to make it work properly.
- You'll have to add an opening route for your
SubModule
which will be a Lazy Loaded Module. You'll do this in your main-routing.module.ts file
const routes: Routes = [
path: 'main',
component: MainComponent,
children: [
path: 'sub',
loadChildren: './sub/sub.module#SubModule'
]
];
- You'll then create a module named
SubModule
and theSubRoutingModule
will act as a routing module for thisSubModule
. You'll declare your Sub1Component and Sub2Component here:
import Sub1Component, Sub2Component from './sub.component';
import NgModule from '@angular/core';
import Routes, RouterModule from '@angular/router';
import SubRoutingModule from './sub-routing.module';
@NgModule(
declarations: [Sub1Component, Sub2Component],
imports: [SubRoutingModule]
)
export class SubModule
So now your routes will change to :
<a routerLink="/main/sub/sub1">Sub1</a> |
<a routerLink="/main/sub/sub2">Sub2</a>
Here's an Updated StackBlitz for your ref.
I need to have multiple routing files at different locations.
– rst
Nov 10 at 17:18
There's a serious architectural issue with your Current Implementation. Can you please share a more concrete example of your modules and probably a minimal stackblitz sample replicating your issue?
– SiddAjmera
Nov 10 at 17:19
Stackblitz example in the op.
– rst
Nov 10 at 17:53
@rst, please go through my updated answer. Hopefully this should help you achieve what you're trying to.
– SiddAjmera
Nov 10 at 18:51
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
There are a few things that you'll have to fix to make it work properly.
- You'll have to add an opening route for your
SubModule
which will be a Lazy Loaded Module. You'll do this in your main-routing.module.ts file
const routes: Routes = [
path: 'main',
component: MainComponent,
children: [
path: 'sub',
loadChildren: './sub/sub.module#SubModule'
]
];
- You'll then create a module named
SubModule
and theSubRoutingModule
will act as a routing module for thisSubModule
. You'll declare your Sub1Component and Sub2Component here:
import Sub1Component, Sub2Component from './sub.component';
import NgModule from '@angular/core';
import Routes, RouterModule from '@angular/router';
import SubRoutingModule from './sub-routing.module';
@NgModule(
declarations: [Sub1Component, Sub2Component],
imports: [SubRoutingModule]
)
export class SubModule
So now your routes will change to :
<a routerLink="/main/sub/sub1">Sub1</a> |
<a routerLink="/main/sub/sub2">Sub2</a>
Here's an Updated StackBlitz for your ref.
There are a few things that you'll have to fix to make it work properly.
- You'll have to add an opening route for your
SubModule
which will be a Lazy Loaded Module. You'll do this in your main-routing.module.ts file
const routes: Routes = [
path: 'main',
component: MainComponent,
children: [
path: 'sub',
loadChildren: './sub/sub.module#SubModule'
]
];
- You'll then create a module named
SubModule
and theSubRoutingModule
will act as a routing module for thisSubModule
. You'll declare your Sub1Component and Sub2Component here:
import Sub1Component, Sub2Component from './sub.component';
import NgModule from '@angular/core';
import Routes, RouterModule from '@angular/router';
import SubRoutingModule from './sub-routing.module';
@NgModule(
declarations: [Sub1Component, Sub2Component],
imports: [SubRoutingModule]
)
export class SubModule
So now your routes will change to :
<a routerLink="/main/sub/sub1">Sub1</a> |
<a routerLink="/main/sub/sub2">Sub2</a>
Here's an Updated StackBlitz for your ref.
edited Nov 10 at 18:51
answered Nov 10 at 16:29
SiddAjmera
12k21137
12k21137
I need to have multiple routing files at different locations.
– rst
Nov 10 at 17:18
There's a serious architectural issue with your Current Implementation. Can you please share a more concrete example of your modules and probably a minimal stackblitz sample replicating your issue?
– SiddAjmera
Nov 10 at 17:19
Stackblitz example in the op.
– rst
Nov 10 at 17:53
@rst, please go through my updated answer. Hopefully this should help you achieve what you're trying to.
– SiddAjmera
Nov 10 at 18:51
add a comment |
I need to have multiple routing files at different locations.
– rst
Nov 10 at 17:18
There's a serious architectural issue with your Current Implementation. Can you please share a more concrete example of your modules and probably a minimal stackblitz sample replicating your issue?
– SiddAjmera
Nov 10 at 17:19
Stackblitz example in the op.
– rst
Nov 10 at 17:53
@rst, please go through my updated answer. Hopefully this should help you achieve what you're trying to.
– SiddAjmera
Nov 10 at 18:51
I need to have multiple routing files at different locations.
– rst
Nov 10 at 17:18
I need to have multiple routing files at different locations.
– rst
Nov 10 at 17:18
There's a serious architectural issue with your Current Implementation. Can you please share a more concrete example of your modules and probably a minimal stackblitz sample replicating your issue?
– SiddAjmera
Nov 10 at 17:19
There's a serious architectural issue with your Current Implementation. Can you please share a more concrete example of your modules and probably a minimal stackblitz sample replicating your issue?
– SiddAjmera
Nov 10 at 17:19
Stackblitz example in the op.
– rst
Nov 10 at 17:53
Stackblitz example in the op.
– rst
Nov 10 at 17:53
@rst, please go through my updated answer. Hopefully this should help you achieve what you're trying to.
– SiddAjmera
Nov 10 at 18:51
@rst, please go through my updated answer. Hopefully this should help you achieve what you're trying to.
– SiddAjmera
Nov 10 at 18:51
add a comment |
up vote
1
down vote
Change forRoot
to forChild
in SubSubRoutingModule
// ./Sub/subsub/SubSub-Routing.module.ts
const routes: Routes = [
path: 'subsub', //<-- this shall route to www.site.com/sub/subsub
component: SubSubComponent,
]
];
@NgModule(
imports: [RouterModule.forChild(routes)], //<-- change here
exports: [RouterModule],
declarations:
)
export class SubSubRoutingModule
That works, butSubSub
(I changed it in the OP to A and B, thought that was more clearer) get routet to the root, i.e./subsub
instead of/Sub/subsub
– rst
Nov 10 at 17:15
You should implement with Lazy Module. Look at this demo for reference stackblitz.com/edit/angular-hcerdo
– Sunil Singh
Nov 10 at 17:46
I have added a stackblitz example
– rst
Nov 10 at 17:52
1
Here is the modified version of your demo stackblitz.com/edit/angular-3mjttd
– Sunil Singh
Nov 10 at 18:04
That's exactly what I was Looking for! Excellent
– rst
Nov 10 at 19:16
add a comment |
up vote
1
down vote
Change forRoot
to forChild
in SubSubRoutingModule
// ./Sub/subsub/SubSub-Routing.module.ts
const routes: Routes = [
path: 'subsub', //<-- this shall route to www.site.com/sub/subsub
component: SubSubComponent,
]
];
@NgModule(
imports: [RouterModule.forChild(routes)], //<-- change here
exports: [RouterModule],
declarations:
)
export class SubSubRoutingModule
That works, butSubSub
(I changed it in the OP to A and B, thought that was more clearer) get routet to the root, i.e./subsub
instead of/Sub/subsub
– rst
Nov 10 at 17:15
You should implement with Lazy Module. Look at this demo for reference stackblitz.com/edit/angular-hcerdo
– Sunil Singh
Nov 10 at 17:46
I have added a stackblitz example
– rst
Nov 10 at 17:52
1
Here is the modified version of your demo stackblitz.com/edit/angular-3mjttd
– Sunil Singh
Nov 10 at 18:04
That's exactly what I was Looking for! Excellent
– rst
Nov 10 at 19:16
add a comment |
up vote
1
down vote
up vote
1
down vote
Change forRoot
to forChild
in SubSubRoutingModule
// ./Sub/subsub/SubSub-Routing.module.ts
const routes: Routes = [
path: 'subsub', //<-- this shall route to www.site.com/sub/subsub
component: SubSubComponent,
]
];
@NgModule(
imports: [RouterModule.forChild(routes)], //<-- change here
exports: [RouterModule],
declarations:
)
export class SubSubRoutingModule
Change forRoot
to forChild
in SubSubRoutingModule
// ./Sub/subsub/SubSub-Routing.module.ts
const routes: Routes = [
path: 'subsub', //<-- this shall route to www.site.com/sub/subsub
component: SubSubComponent,
]
];
@NgModule(
imports: [RouterModule.forChild(routes)], //<-- change here
exports: [RouterModule],
declarations:
)
export class SubSubRoutingModule
answered Nov 10 at 16:37
Sunil Singh
6,1121626
6,1121626
That works, butSubSub
(I changed it in the OP to A and B, thought that was more clearer) get routet to the root, i.e./subsub
instead of/Sub/subsub
– rst
Nov 10 at 17:15
You should implement with Lazy Module. Look at this demo for reference stackblitz.com/edit/angular-hcerdo
– Sunil Singh
Nov 10 at 17:46
I have added a stackblitz example
– rst
Nov 10 at 17:52
1
Here is the modified version of your demo stackblitz.com/edit/angular-3mjttd
– Sunil Singh
Nov 10 at 18:04
That's exactly what I was Looking for! Excellent
– rst
Nov 10 at 19:16
add a comment |
That works, butSubSub
(I changed it in the OP to A and B, thought that was more clearer) get routet to the root, i.e./subsub
instead of/Sub/subsub
– rst
Nov 10 at 17:15
You should implement with Lazy Module. Look at this demo for reference stackblitz.com/edit/angular-hcerdo
– Sunil Singh
Nov 10 at 17:46
I have added a stackblitz example
– rst
Nov 10 at 17:52
1
Here is the modified version of your demo stackblitz.com/edit/angular-3mjttd
– Sunil Singh
Nov 10 at 18:04
That's exactly what I was Looking for! Excellent
– rst
Nov 10 at 19:16
That works, but
SubSub
(I changed it in the OP to A and B, thought that was more clearer) get routet to the root, i.e. /subsub
instead of /Sub/subsub
– rst
Nov 10 at 17:15
That works, but
SubSub
(I changed it in the OP to A and B, thought that was more clearer) get routet to the root, i.e. /subsub
instead of /Sub/subsub
– rst
Nov 10 at 17:15
You should implement with Lazy Module. Look at this demo for reference stackblitz.com/edit/angular-hcerdo
– Sunil Singh
Nov 10 at 17:46
You should implement with Lazy Module. Look at this demo for reference stackblitz.com/edit/angular-hcerdo
– Sunil Singh
Nov 10 at 17:46
I have added a stackblitz example
– rst
Nov 10 at 17:52
I have added a stackblitz example
– rst
Nov 10 at 17:52
1
1
Here is the modified version of your demo stackblitz.com/edit/angular-3mjttd
– Sunil Singh
Nov 10 at 18:04
Here is the modified version of your demo stackblitz.com/edit/angular-3mjttd
– Sunil Singh
Nov 10 at 18:04
That's exactly what I was Looking for! Excellent
– rst
Nov 10 at 19:16
That's exactly what I was Looking for! Excellent
– rst
Nov 10 at 19:16
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%2f53240971%2fdefining-relative-routing-in-angular%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
RouterModule.forRoot()
is used for routing only in the root module of the application. For using routes in other modules, useRouterModule.forChild()
– Sachin Gupta
Nov 10 at 16:56