相对路径。 baseUrl和路径不能在ionic2上工作 - angular2

问题描述:

我一直在阅读类似的堆栈溢出,但我一直无法弄清楚。我必须错过一小步。相对路径。 baseUrl和路径不能在ionic2上工作 - angular2

我的目标是能够做到:的

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代码不会抱怨我的导入。

//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

据我所知,如果你从同一个项目导入你自己的模块,你需要使用相对url。如果它是从npm安装的,那么它只会导入没有相对url的文件,这将是'package.json'中列出的模块。 – adriancarriger

+1

尝试在''app''中使用'tsc --traceResolution'' – raj

+0

https://paste.ee/p/bXxZx – yafrack

我怀疑moduleResolution设置为node可能会导致问题。将其更改为classic并查看它是否已解决。

"compilerOptions": { 
    "moduleResolution": "classic" 

看看这个issue。希望它解决了这个错误。

+0

嗨。谢谢。我自己并没有补充说,它是离子装置的一部分。事情是,将它改为经典可以打破一切。即:无法找到名称'Observable',无法找到名称'AngularFireDatabase'等。 – yafrack

+0

没有路径包含node_modules文件夹? – raj

+0

是的,它被添加:/ – yafrack