Angular 库路由模块在应用程序中导入时不起作用
Angular Library Route Module does not work when imported in application
我需要帮助解决我无法解决的问题。
我正在研究 angular 7 库,以便将模块化添加到我的应用程序中,但是在库中定义的模块路由在应用程序中安装时似乎不起作用。
我已经创建项目 github 来重现问题:
图书馆测试foo-lib
外部应用程序foo-tester
在每个自述文件中,我都描述了如何重现此问题。
我创建了一个示例库 (foo-lib),其路由模块定义了子路由,然后我在同一工作区中的一个简单应用程序中构建并测试了它。
一切正常,库路由已正确添加到测试应用程序。
在下一步中,我导出构建的库,并通过 npm link 命令将其包含在另一个工作区的另一个应用程序示例中,并在此应用程序上运行 ng serve 命令。
使用此配置,如果尝试实现库路由路径,我会收到错误消息,就好像它们没有添加到主路由模块中一样。
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 {}
尝试导航到图书馆路线时控制台中显示错误
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)
终于找到了解决方案。我在下面描述它以帮助社区。
在这种情况下,我们需要在构建部分添加 "preserveSymlinks": true 选项到主应用程序 angular.json,即通过 npm link 命令导入外部库的那个。
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"preserveSymlinks": true,
致谢名单 filipesilva on angular-cli issues
希望这对以后的其他人有帮助。
谢谢
我有一个 angular 11 项目,我修复了这个错误。我在 tsconfig.lib.prod.json
中的 angularCompilerOptions
中启用了 enableIvy
并且我使用 --prod
.
进行了构建
"angularCompilerOptions": {
"enableIvy": true
}
我需要帮助解决我无法解决的问题。 我正在研究 angular 7 库,以便将模块化添加到我的应用程序中,但是在库中定义的模块路由在应用程序中安装时似乎不起作用。
我已经创建项目 github 来重现问题:
图书馆测试foo-lib
外部应用程序foo-tester
在每个自述文件中,我都描述了如何重现此问题。
我创建了一个示例库 (foo-lib),其路由模块定义了子路由,然后我在同一工作区中的一个简单应用程序中构建并测试了它。 一切正常,库路由已正确添加到测试应用程序。 在下一步中,我导出构建的库,并通过 npm link 命令将其包含在另一个工作区的另一个应用程序示例中,并在此应用程序上运行 ng serve 命令。 使用此配置,如果尝试实现库路由路径,我会收到错误消息,就好像它们没有添加到主路由模块中一样。
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 {}
尝试导航到图书馆路线时控制台中显示错误
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)
终于找到了解决方案。我在下面描述它以帮助社区。 在这种情况下,我们需要在构建部分添加 "preserveSymlinks": true 选项到主应用程序 angular.json,即通过 npm link 命令导入外部库的那个。
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"preserveSymlinks": true,
致谢名单 filipesilva on angular-cli issues
希望这对以后的其他人有帮助。
谢谢
我有一个 angular 11 项目,我修复了这个错误。我在 tsconfig.lib.prod.json
中的 angularCompilerOptions
中启用了 enableIvy
并且我使用 --prod
.
"angularCompilerOptions": {
"enableIvy": true
}