Angular Library Route Module does not work when imported in application

孤街醉人 提交于 2020-01-21 12:42:35

问题


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)

回答1:


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



来源:https://stackoverflow.com/questions/54317993/angular-library-route-module-does-not-work-when-imported-in-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!