我一直在阅读类似的堆栈溢出,但我还没能弄明白。我一定错过了一小步。
我的目标是能够做到:
import { Logger } from 'logging'
而不是
import { Logger } from '../../modules/logging'
我的tsconfig看起来像这样:
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"dom",
"es2015"
],
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"target": "es5",
"baseUrl": ".",
"paths":{
"*":[
"src/app/*",
"node_modules/*"
]
}
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
],
"compileOnSave": false,
"atom": {
"rewriteTsconfig": false
}
}
我的文件夹结构类似于
src
--- app
------- logging
--------------- index.ts (contains exports for the provider and the ngModule)
--------------- logger.provider.ts (contains an injectable provider)
--------------- logging.module.ts (contains the ngModule)
--- app.module.ts
索引基本上有这个:
export { LoggingModule } from './logging.module';
export { LoggerProvider } from './logger.provider';
当然我从结构中省略了文件。 app.module.ts是我的入门课程,我试图导入我的日志记录'模块。 Visual Studio Code并不抱怨我的导入。
//import { FIREBASE_CONFIG } from 'configs';
import { NgModule, ErrorHandler } from '@angular/core';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { AngularFireModule } from "angularfire2";
import { LoggingModule } from 'logging';
import { PostsModule } from 'posts';
@NgModule({
declarations: [
MyApp,
HomePage
],
imports: [
IonicModule.forRoot(MyApp),
PostsModule,
LoggingModule
//AngularFireModule.initializeApp(FIREBASE_CONFIG),
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage
],
providers: [{provide: ErrorHandler, useClass: IonicErrorHandler}]
})
export class AppModule {}
正如我所说,VSC没有显示任何错误,但当我在浏览器中启动应用程序时,我得到:
Error: Cannot find module "logging"
at Object.<anonymous> (http://localhost:8100/build/main.js:82202:7)
at __webpack_require__ (http://localhost:8100/build/main.js:20:30)
at Object.<anonymous> (http://localhost:8100/build/main.js:105047:70)
at __webpack_require__ (http://localhost:8100/build/main.js:20:30)
at http://localhost:8100/build/main.js:66:18
at http://localhost:8100/build/main.js:69:10
我真的不想回到漫长的相对路线上,因为1)它很乏味,2)当应用程序增长时,它会让重构成为一场噩梦。
谢谢!
答案 0 :(得分:0)