I need a help about an issue that I cannot solve. I'm studying angular 7 library in order to add modularization to my application but module routes defined in library seems not work when install it in a application.
I have created project to github to reproduce problem:
Library test foo-lib
External Applicationfoo-tester
In each ReadMe file I have described how to reproduce this issue.
I have created an example library (foo-lib) with its routing module with child route defined, then I had builded and tested it in a simple application in the same workspace. All worked fine, and library routing is correctly added to test application. On the next step I exported builded library and included it in another application example in another workspace by npm link command, and runed ng serve command on this app. With this configuration I received an error if try to achieve library route paths, as if they was not added to main route module.
Foo-lib-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { MainComponent } from './components/main/main.component';
import { NewFooCmpComponent } from './components/new-foo-cmp/new-foo-cmp.component';
const UP_ROUTES: Routes = [
{ path: 'main_path', component: MainComponent,
children: [
{ path: 'child', component: NewFooCmpComponent }
]
}
];
@NgModule({
imports: [RouterModule.forChild(UP_ROUTES)],
exports: [RouterModule]
})
export class FooLibRoutingModule { }
Foo-lib.module.ts
import { NgModule } from '@angular/core';
import { FooLibRoutingModule } from './foo-lib-routing.module';
import { MainComponent } from './components/main/main.component';
import { FooLibComponent } from './foo-lib.component';
import { NewFooCmpComponent } from './components/new-foo-cmp/new-foo-cmp.component';
@NgModule({
declarations: [
MainComponent,
FooLibComponent,
NewFooCmpComponent],
imports: [
FooLibRoutingModule
],
exports: [
FooLibComponent,
NewFooCmpComponent]
})
export class FooLibModule { }
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RoutingModule } from './routing.module';
import { AppComponent } from './app.component';
import { WelcomeComponent } from './components/welcome/welcome.component';
import { NotFoundComponent } from './components/not-found/not-found.component';
import { FooLibModule } from '@foo/lib';
@NgModule({
declarations: [
AppComponent,
WelcomeComponent,
NotFoundComponent
],
imports: [
BrowserModule,
RoutingModule,
FooLibModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { WelcomeComponent } from './components/welcome/welcome.component';
import { NotFoundComponent } from './components/not-found/not-found.component';
const APP_ROUTES: Routes = [
{ path: 'welcome', component: WelcomeComponent },
{ path: 'notfound', component: NotFoundComponent },
{ path: '', redirectTo: '/welcome', pathMatch: 'full' },
];
/**
* Main client routing configuration module
*/
@NgModule({
imports: [ RouterModule.forRoot(APP_ROUTES) ],
exports: [ RouterModule ]
})
export class RoutingModule {}
Error show in console when try to navigate to library route
core.js:15714 ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'main_path/child'
Error: Cannot match any routes. URL Segment: 'main_path/child'
at ApplyRedirects.push../node_modules/@angular/router/fesm5/router.js.ApplyRedirects.noMatchError (router.js:2469)
Every Angular application has at least one module, the root module. You bootstrap that module to launch the application. The root module is all you need in an application with few components.
In Angular, the best practice is to load and configure the router in a separate, top-level module. The router is dedicated to routing and imported by the root AppModule . By convention, the module class name is AppRoutingModule and it belongs in the app-routing.
No, the Routing Module is a design choice. You can skip routing Module (for example, AppRoutingModule) when the configuration is simple and merge the routing configuration directly into the companion module (for example, AppModule).
Finally I founded a solution. I describe it below in order to help community. In this scenario we need to add "preserveSymlinks": true option in build sectionto main application angular.json, the one that imported external library by npm link command.
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"preserveSymlinks": true,
Credits filipesilva on angular-cli issues
Hope this can help someone else in future.
Thanks
I have an angular 11 project and I fixed this error. I enabled enableIvy
in angularCompilerOptions
in the tsconfig.lib.prod.json
and I have built with --prod
.
"angularCompilerOptions": {
"enableIvy": true
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With