我正在尝试模块化我的路由模块,如下所示:
app.modules.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './main/app.component';
import { MaterialModule } from '@angular/material';
import { RouterModule, Routes } from '@angular/router';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
import { AppRoute } from './app.routes';
@NgModule({
declarations: [
AppComponent,
PageNotFoundComponent
],
imports: [
MaterialModule,
BrowserModule,
FormsModule,
HttpModule,
AppRoute
],
providers: [
UserService
],
bootstrap: [ AppComponent ],
})
export class AppModule { }
应用程序.routes.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
const appRoutes: Routes = [
{ path: '', redirectTo: '/', pathMatch: 'full' },
{ path: '**', component: PageNotFoundComponent }
]
@NgModule({
imports: [
RouterModule.forRoot(appRoutes)
],
exports: [
RouterModule
]
})
export class AppRoute { }
此方法运行良好,但我一直认为我已经在两个文件上指定了组件导入,例如那个 page-not-found。有没有办法将组件导入模块化到单独的文件中,以便我可以将该文件包含在模块和路由中?我已经尝试过这些:
应用程序.routes.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AppComponent } from './main/app.component';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
const appRoutes: Routes = [
{ path: '', redirectTo: '/', pathMatch: 'full' },
{ path: '**', component: PageNotFoundComponent }
]
@NgModule({
declarations: [
AppComponent,
PageNotFoundComponent
],
imports: [
MaterialModule,
RouterModule.forRoot(appRoutes)
],
exports: [
RouterModule
]
})
export class AppRoute { }
并从 app.modules.ts 中删除多余的声明,但似乎 angular 找不到 ngIf 和 md-icon。
我的目标是在克隆这个骨架时尽量减少用户必须编辑的文件数量以满足他们的需要。我正在为我未来的项目创建一个骨架,并且我想最小化我必须编辑的文件。如果可能,我不想碰 app.modules.ts,只编辑 app.routes.ts 和 app.config.ts。或者更好的是,只编辑 app.config.ts,然后放入const appRoutes
app.config.ts。这甚至可能吗?什么是最小化编辑的最佳方法,这样我们就不需要对不必要的文件进行大量篡改。
谢谢你的帮忙