在应用程序中导入时,Angular Library Route Module不起作用 [英] Angular Library Route Module does not work when imported in application

查看:178
本文介绍了在应用程序中导入时,Angular Library Route Module不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关无法解决的问题的帮助。
我正在研究angular 7库,以便为应用程序添加模块化,但是在库中定义的模块路由在应用程序中安装时似乎无法使用。

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.

我已经创建了一个到github的项目来重现问题:

库测试 foo-lib

外部应用程序 foo-tester

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.

我创建了一个示例库(foo-lib),其路由模块已定义了子路由,然后在同一工作区中的一个简单应用程序中对其进行了构建和测试。
一切正常,并且库路由已正确添加到测试应用程序。
下一步,我导出构建的库,并通过npm link命令将其包含在另一个工作区的另一个应用示例中,并在此应用上运行ng serve命令。
使用这种配置,如果尝试获取库路由路径,就好像没有将它们添加到主路由模块中一样,则会收到错误消息。

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

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

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

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

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)


推荐答案

最后,我建立了一个解决方案。我在下面描述它以帮助社区。
在这种情况下,我们需要在构建部分的主应用程序angular.json中添加 preserveSymlinks:true选项,该应用程序是通过npm link命令导入外部库的。

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,

贷方 filipesilva处理angular-cli问题

希望这对以后有帮助。

谢谢

这篇关于在应用程序中导入时,Angular Library Route Module不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆