我正在测试自定义名称空间,以使import
更好:
所有组件都添加到文件夹app\components\
文件conponents.module.ts
:
...
import { DashboardComponent } from './dashboard/dashboard.component';
@NgModule({
imports: [
CommonModule,
RouterModule,
FormsModule,
ServicesModule
],
declarations: [
DashboardComponent,
...
],
exports:[
MessagesComponent
]
})
export class ComponentsModule { }
将paths
添加到了tsconfig.ts
:
"baseUrl": "./src",
"paths":{
"@beewest/components/*" : ["app/components/*"]
},
如果将路径修改为: components.module.ts
失败,并显示错误ERROR in src/app/components/components.module.ts(11,36): error TS2307: Cannot find module '@beewest/components'.
import { DashboardComponent } from '@beewest/components/dashboard/dashboard.component';
Visual Studio代码未显示任何错误,但是ng serve
返回了error TS2307: Cannot find module '@beewest/components/dashboard/dashboard.component'
。
有没有建议?
答案 0 :(得分:0)
好像paths
是在重启ng serve
之后更新的。
@beewest/...
根据配置的app/...
解析为paths
:
import { DashboardComponent } from '@beewest/components/dashboard/dashboard.component';
要删除.../dashboard/dashboard.component
,打字稿的module resolution需要将文件index.ts
添加到文件夹app/components
中,并带有export
行:
export {DashboardComponent} from './dashboard/dashboard.component';
Nicer导入行:
import { DashboardComponent } from '@beewest/components';