Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Library Route Module does not work when imported in application

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)
like image 242
rvt Avatar asked Jan 22 '19 23:01

rvt


People also ask

Which modules should be imported for Angular routing?

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.

Which module do we need to import for enabling router in Angular application?

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.

Do I need a routing module always in Angular?

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).


2 Answers

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

like image 66
rvt Avatar answered Nov 15 '22 06:11

rvt


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
  }
like image 33
Salih KARAHAN Avatar answered Nov 15 '22 06:11

Salih KARAHAN