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

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

问题描述

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

我已将项目创建到 github 以重现问题:
库测试 foo-lib
外部应用foo-tester

在每个自述文件中,我都描述了如何重现此问题.

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

Foo-lib-routing.module.ts

import { NgModule } from '@angular/core';从'@angular/router' 导入 { Routes, RouterModule };import { MainComponent } from './components/main/main.component';import { NewFooCmpComponent } from './components/new-foo-cmp/new-foo-cmp.component';const UP_ROUTES: 路由 = [{ 路径:'main_path',组件:MainComponent,孩子们: [{ 路径:'子',组件:NewFooCmpComponent }]}];@NgModule({进口:[RouterModule.forChild(UP_ROUTES)],出口:[路由器模块]})导出类 FooLibRoutingModule { }

Foo-lib.module.ts

<预><代码>从'@angular/core' 导入 { NgModule };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({声明: [主要组件,FooLib 组件,NewFooCmpComponent],进口:[FooLib路由模块],出口:[FooLib 组件,新建FooCmp组件]})导出类 FooLibModule { }

app.module.ts

import { BrowserModule } from '@angular/platform-b​​rowser';从'@angular/core' 导入 { NgModule };从'./routing.module'导入{RoutingModule};从 './app.component' 导入 { AppComponent };import { WelcomeComponent } from './components/welcome/welcome.component';import { NotFoundComponent } from './components/not-found/not-found.component';从'@foo/lib' 导入 { FooLibModule };@NgModule({声明: [应用组件,欢迎组件,未找到组件],进口:[浏览器模块,路由模块,FooLib模块],提供者:[],引导程序:[AppComponent]})导出类 AppModule { }

routing.module.ts

<预><代码>从'@angular/core' 导入 { NgModule };从@angular/router"导入 { RouterModule, Routes };import { WelcomeComponent } from './components/welcome/welcome.component';import { NotFoundComponent } from './components/not-found/not-found.component';const APP_ROUTES: 路由 = [{ path: 'welcome', 组件: WelcomeComponent },{ path: 'notfound', 组件: NotFoundComponent },{ path: '', redirectTo: '/welcome', pathMatch: 'full' },];/*** 主客户端路由配置模块*/@NgModule({进口:[ RouterModule.forRoot(APP_ROUTES) ],出口:[ RouterModule ]})导出类 RoutingModule {}

尝试导航到库路由时在控制台中显示错误

core.js:15714 错误错误:未捕获(承诺):错误:无法匹配任何路由.URL 段:'main_path/child'错误:无法匹配任何路由.URL 段:'main_path/child'在 ApplyRedirects.push../node_modules/@angular/router/fesm5/router.js.ApplyRedirects.noMatchError (router.js:2469)

解决方案

我终于找到了解决方案.我在下面描述它以帮助社区.在这种情况下,我们需要将 build 部分中的 "preserveSymlinks": true 选项添加到主应用程序 angular.json 中,即通过 npm link 命令导入外部库的那个.

架构师":{建造": {"builder": "@angular-devkit/build-angular:browser",选项": {保留符号链接":真,

感谢 filipesilva 关于 angular-cli 问题

希望这可以在未来帮助其他人.

谢谢

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)

解决方案

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

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

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