Angular Routes: Problem defining a NotFoundComponent with multiple routings files





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I am creating a NotFoundComponent so that every non-existing url, a friendly page will apear to the user.



My Angular 6 project has 5 features(CRUDL):



CompanyComponent,
EmployeeComponent,
BranchComponent,
BenefitsComponent,
MainComponent



Each component have it own .module and .routing, for example the company.routing.ts defines the route like this:



    export const routes: Routes = [
{
path: "branch",
component: EmptyComponent,
}
...
@NgModule({
imports: [
RouterModule.forChild(routes),
...


it also defines its childrens, like path: "branch/:id".



So, i have no problem with the way i built the routing.



The problem I am facing now is the way I can define a path to the NotFoundComponent.



Because I don't have a single file where I define my routes, if I put the definition of the /404 page on the on the bottom of routers definition in the main.routing.ts, every other route defined on the features(like the company.routing.ts) will be redirected to the /404, since angular uses the first match strategy.



Things i tried so far:



on the main.routing.ts:



export const routes: Routes = [
{
path: "main",
component: MainComponent,
},
{
path: "404",
resolve: {
routeTitle: TitleResolver,
},
component: NotFoundComponent,
},
{
path: "",
pathMatch: "full",
redirectTo: "/main",
},
{
path: "**",
redirectTo: "/404",
}


if I put this way, when I try accessing the route /company (defined in the company.routing.ts) it will be redirected to the /404.



I also tried defining it on any a routing of one of my features, like on the company.routing.ts



{
path: "**",
redirectTo: "/404",
}


Than, a funny thing happened, if I define this piece of code on the benefits.routing.ts, i can access /benefits but /branch, /employee and the rest of the routes will be redirected to /404.



project structure



if I define it on branch, I can access /benefits, /branch but /employee and the rest of the routes will be redirected to /404.



I could just place this code on the last feature loaded and it would work but I would like to know if I can solve this with another approach, can't I define a route that I want to be the last one loaded?



thanks in advance.










share|improve this question























  • Need to see how you load your other modules using route - stackblitz will do great - please add

    – Rahul
    Nov 23 '18 at 19:12











  • @Gustavo. The link below may solve your issues:stackoverflow.com/questions/49621578/…. It explains lazy loading and children routes. Each module has its own <router-outlet>. You can have lots of modules and you should import them like a tree structure.

    – harold_mean2
    Nov 23 '18 at 20:02











  • my problem is because I lazy load modules, but I can't change the way I import stuffs because otherwise it will be a performance loss, can't find any work around yet, I probably would need to define my routes elsewhere and import them.

    – Gustavo
    Nov 26 '18 at 19:19


















1















I am creating a NotFoundComponent so that every non-existing url, a friendly page will apear to the user.



My Angular 6 project has 5 features(CRUDL):



CompanyComponent,
EmployeeComponent,
BranchComponent,
BenefitsComponent,
MainComponent



Each component have it own .module and .routing, for example the company.routing.ts defines the route like this:



    export const routes: Routes = [
{
path: "branch",
component: EmptyComponent,
}
...
@NgModule({
imports: [
RouterModule.forChild(routes),
...


it also defines its childrens, like path: "branch/:id".



So, i have no problem with the way i built the routing.



The problem I am facing now is the way I can define a path to the NotFoundComponent.



Because I don't have a single file where I define my routes, if I put the definition of the /404 page on the on the bottom of routers definition in the main.routing.ts, every other route defined on the features(like the company.routing.ts) will be redirected to the /404, since angular uses the first match strategy.



Things i tried so far:



on the main.routing.ts:



export const routes: Routes = [
{
path: "main",
component: MainComponent,
},
{
path: "404",
resolve: {
routeTitle: TitleResolver,
},
component: NotFoundComponent,
},
{
path: "",
pathMatch: "full",
redirectTo: "/main",
},
{
path: "**",
redirectTo: "/404",
}


if I put this way, when I try accessing the route /company (defined in the company.routing.ts) it will be redirected to the /404.



I also tried defining it on any a routing of one of my features, like on the company.routing.ts



{
path: "**",
redirectTo: "/404",
}


Than, a funny thing happened, if I define this piece of code on the benefits.routing.ts, i can access /benefits but /branch, /employee and the rest of the routes will be redirected to /404.



project structure



if I define it on branch, I can access /benefits, /branch but /employee and the rest of the routes will be redirected to /404.



I could just place this code on the last feature loaded and it would work but I would like to know if I can solve this with another approach, can't I define a route that I want to be the last one loaded?



thanks in advance.










share|improve this question























  • Need to see how you load your other modules using route - stackblitz will do great - please add

    – Rahul
    Nov 23 '18 at 19:12











  • @Gustavo. The link below may solve your issues:stackoverflow.com/questions/49621578/…. It explains lazy loading and children routes. Each module has its own <router-outlet>. You can have lots of modules and you should import them like a tree structure.

    – harold_mean2
    Nov 23 '18 at 20:02











  • my problem is because I lazy load modules, but I can't change the way I import stuffs because otherwise it will be a performance loss, can't find any work around yet, I probably would need to define my routes elsewhere and import them.

    – Gustavo
    Nov 26 '18 at 19:19














1












1








1


0






I am creating a NotFoundComponent so that every non-existing url, a friendly page will apear to the user.



My Angular 6 project has 5 features(CRUDL):



CompanyComponent,
EmployeeComponent,
BranchComponent,
BenefitsComponent,
MainComponent



Each component have it own .module and .routing, for example the company.routing.ts defines the route like this:



    export const routes: Routes = [
{
path: "branch",
component: EmptyComponent,
}
...
@NgModule({
imports: [
RouterModule.forChild(routes),
...


it also defines its childrens, like path: "branch/:id".



So, i have no problem with the way i built the routing.



The problem I am facing now is the way I can define a path to the NotFoundComponent.



Because I don't have a single file where I define my routes, if I put the definition of the /404 page on the on the bottom of routers definition in the main.routing.ts, every other route defined on the features(like the company.routing.ts) will be redirected to the /404, since angular uses the first match strategy.



Things i tried so far:



on the main.routing.ts:



export const routes: Routes = [
{
path: "main",
component: MainComponent,
},
{
path: "404",
resolve: {
routeTitle: TitleResolver,
},
component: NotFoundComponent,
},
{
path: "",
pathMatch: "full",
redirectTo: "/main",
},
{
path: "**",
redirectTo: "/404",
}


if I put this way, when I try accessing the route /company (defined in the company.routing.ts) it will be redirected to the /404.



I also tried defining it on any a routing of one of my features, like on the company.routing.ts



{
path: "**",
redirectTo: "/404",
}


Than, a funny thing happened, if I define this piece of code on the benefits.routing.ts, i can access /benefits but /branch, /employee and the rest of the routes will be redirected to /404.



project structure



if I define it on branch, I can access /benefits, /branch but /employee and the rest of the routes will be redirected to /404.



I could just place this code on the last feature loaded and it would work but I would like to know if I can solve this with another approach, can't I define a route that I want to be the last one loaded?



thanks in advance.










share|improve this question














I am creating a NotFoundComponent so that every non-existing url, a friendly page will apear to the user.



My Angular 6 project has 5 features(CRUDL):



CompanyComponent,
EmployeeComponent,
BranchComponent,
BenefitsComponent,
MainComponent



Each component have it own .module and .routing, for example the company.routing.ts defines the route like this:



    export const routes: Routes = [
{
path: "branch",
component: EmptyComponent,
}
...
@NgModule({
imports: [
RouterModule.forChild(routes),
...


it also defines its childrens, like path: "branch/:id".



So, i have no problem with the way i built the routing.



The problem I am facing now is the way I can define a path to the NotFoundComponent.



Because I don't have a single file where I define my routes, if I put the definition of the /404 page on the on the bottom of routers definition in the main.routing.ts, every other route defined on the features(like the company.routing.ts) will be redirected to the /404, since angular uses the first match strategy.



Things i tried so far:



on the main.routing.ts:



export const routes: Routes = [
{
path: "main",
component: MainComponent,
},
{
path: "404",
resolve: {
routeTitle: TitleResolver,
},
component: NotFoundComponent,
},
{
path: "",
pathMatch: "full",
redirectTo: "/main",
},
{
path: "**",
redirectTo: "/404",
}


if I put this way, when I try accessing the route /company (defined in the company.routing.ts) it will be redirected to the /404.



I also tried defining it on any a routing of one of my features, like on the company.routing.ts



{
path: "**",
redirectTo: "/404",
}


Than, a funny thing happened, if I define this piece of code on the benefits.routing.ts, i can access /benefits but /branch, /employee and the rest of the routes will be redirected to /404.



project structure



if I define it on branch, I can access /benefits, /branch but /employee and the rest of the routes will be redirected to /404.



I could just place this code on the last feature loaded and it would work but I would like to know if I can solve this with another approach, can't I define a route that I want to be the last one loaded?



thanks in advance.







javascript angular typescript angular6






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 17:45









GustavoGustavo

689




689













  • Need to see how you load your other modules using route - stackblitz will do great - please add

    – Rahul
    Nov 23 '18 at 19:12











  • @Gustavo. The link below may solve your issues:stackoverflow.com/questions/49621578/…. It explains lazy loading and children routes. Each module has its own <router-outlet>. You can have lots of modules and you should import them like a tree structure.

    – harold_mean2
    Nov 23 '18 at 20:02











  • my problem is because I lazy load modules, but I can't change the way I import stuffs because otherwise it will be a performance loss, can't find any work around yet, I probably would need to define my routes elsewhere and import them.

    – Gustavo
    Nov 26 '18 at 19:19



















  • Need to see how you load your other modules using route - stackblitz will do great - please add

    – Rahul
    Nov 23 '18 at 19:12











  • @Gustavo. The link below may solve your issues:stackoverflow.com/questions/49621578/…. It explains lazy loading and children routes. Each module has its own <router-outlet>. You can have lots of modules and you should import them like a tree structure.

    – harold_mean2
    Nov 23 '18 at 20:02











  • my problem is because I lazy load modules, but I can't change the way I import stuffs because otherwise it will be a performance loss, can't find any work around yet, I probably would need to define my routes elsewhere and import them.

    – Gustavo
    Nov 26 '18 at 19:19

















Need to see how you load your other modules using route - stackblitz will do great - please add

– Rahul
Nov 23 '18 at 19:12





Need to see how you load your other modules using route - stackblitz will do great - please add

– Rahul
Nov 23 '18 at 19:12













@Gustavo. The link below may solve your issues:stackoverflow.com/questions/49621578/…. It explains lazy loading and children routes. Each module has its own <router-outlet>. You can have lots of modules and you should import them like a tree structure.

– harold_mean2
Nov 23 '18 at 20:02





@Gustavo. The link below may solve your issues:stackoverflow.com/questions/49621578/…. It explains lazy loading and children routes. Each module has its own <router-outlet>. You can have lots of modules and you should import them like a tree structure.

– harold_mean2
Nov 23 '18 at 20:02













my problem is because I lazy load modules, but I can't change the way I import stuffs because otherwise it will be a performance loss, can't find any work around yet, I probably would need to define my routes elsewhere and import them.

– Gustavo
Nov 26 '18 at 19:19





my problem is because I lazy load modules, but I can't change the way I import stuffs because otherwise it will be a performance loss, can't find any work around yet, I probably would need to define my routes elsewhere and import them.

– Gustavo
Nov 26 '18 at 19:19












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451032%2fangular-routes-problem-defining-a-notfoundcomponent-with-multiple-routings-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451032%2fangular-routes-problem-defining-a-notfoundcomponent-with-multiple-routings-file%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?