How can I organize my Angular app folders like a Java package?

倾然丶 夕夏残阳落幕 提交于 2019-11-30 15:31:14

问题


How to organize Angular 2 app folder structure like Java packages?

Consider the following project layout:

app
 |_model
 |_component
 |_service

I would like to import foo.service.ts from service to bar.component.ts in component. But as far as I know, Angular 2 import supports only relative paths like /../service/, which seems very clunky solution.

Is there a way to refer folders with absolute bath from root folder, like with Java packages?


回答1:


UPDATE 2016-06-01

using npm install typescript@next you can already use this function

I assume that your tree looks like this:

node_modules
 |_@angular
 |_rxjs
app
 |_model
 |_component
 |_service
package.json
tsconfig.json

You should add in your tsconfig.json the following lines:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "*": [
        "app/*",
        "node_modules/*"
      ]
    }
  }
}

then you can reference your modules like you do with regular @angular/rxjs modules

import { MyService } from 'service/MyService';
import { Component } from '@angular/core';
import { Observable } from 'rxjs';

Note: webpack needs also the following lines in webpack.config.js

resolve: {
    modulesDirectories: [
        'node_modules',
        'app'
    ]
}



回答2:


Not yet, it should be present soon with Typescript 2.0

Look here https://github.com/Microsoft/TypeScript/issues/5039



来源:https://stackoverflow.com/questions/37547364/how-can-i-organize-my-angular-app-folders-like-a-java-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!