typescript - 如何从 Angular2 TypeScript 中的模块文件夹加载多个类?

标签 typescript module angular

我有一个具有以下模块结构的 Angular2 应用程序:

/app
    /content
        /models
            resource.ts
            container.ts
            entity-type.ts
            index.ts

        /services
            /whatever
                whatever.service.ts

我的模型 index.ts 看起来像:

export * from './resource';
export * from './container';
export * from './entity-type';

我希望能够将所有模型加载到 whatever.service.ts 中。

import {Resource, Container} from '../../models';

我的 system-config.js 文件的桶加载部分如下所示:

const barrels: string[] = [
  // Angular specific barrels.
  '@angular/core',
  '@angular/common',
  '@angular/compiler',
  '@angular/http',
  '@angular/router',
  '@angular/platform-browser',
  '@angular/platform-browser-dynamic',

  // Thirdparty barrels.
  'rxjs',

  // App specific barrels.
  'app',
  'app/shared',
  'app/content',
  /** @cli-barrel */
];

TypeScript 编译它没有错误,但是,在浏览器中,我从系统加载器和区域中收到以下错误,提示找不到某些文件。

GET http://localhost:4200/app/content/models.js 404 (Not Found)scheduleTask @ zone.js:101ZoneDelegate.scheduleTask @ zone.js:336Zone.scheduleMacroTask @ zone.js:273(anonymous function) @ zone.js:122send @ VM59771:3fetchTextFromURL @ system.src.js:1154(anonymous function) @ system.src.js:1735ZoneAwarePromise @ zone.js:584(anonymous function) @ system.src.js:1734(anonymous function) @ system.src.js:2759(anonymous function) @ system.src.js:3333(anonymous function) @ system.src.js:3600(anonymous function) @ system.src.js:3985(anonymous function) @ system.src.js:4448(anonymous function) @ system.src.js:4700(anonymous function) @ system.src.js:406ZoneDelegate.invoke @ zone.js:323Zone.run @ zone.js:216(anonymous function) @ zone.js:571ZoneDelegate.invokeTask @ zone.js:356Zone.runTask @ zone.js:256drainMicroTaskQueue @ zone.js:474ZoneTask.invoke @ zone.js:426 zone.js:461 Unhandled Promise rejection: Error: XHR error (404 Not Found) loading http://localhost:4200/app/content/models.js at XMLHttpRequest.wrapFn [as _onreadystatechange] (http://localhost:4200/vendor/zone.js/dist/zone.js:769:30) at ZoneDelegate.invokeTask (http://localhost:4200/vendor/zone.js/dist/zone.js:356:38) at Zone.runTask (http://localhost:4200/vendor/zone.js/dist/zone.js:256:48) at XMLHttpRequest.ZoneTask.invoke (http://localhost:4200/vendor/zone.js/dist/zone.js:423:34) Error loading http://localhost:4200/app/content/models.js as "../../models" from http://localhost:4200/app/content/services/container/container.service.js ; Zone: ; Task: Promise.then ; Value: Error: Error: XHR error (404 Not Found) loading http://localhost:4200/app/content/models.js(…)consoleError @ zone.js:461_loop_1 @ zone.js:490drainMicroTaskQueue @ zone.js:494ZoneTask.invoke @ zone.js:426 zone.js:463 Error: Uncaught (in promise): Error: Error: XHR error (404 Not Found) loading http://localhost:4200/app/content/models.js(…)

当我直接从它的 .ts 文件导入每个模型时,一切正常。

import { EntityType } from '../../models/entity-type';
import { Container } from '../../models/container';

如何在 Angular2 中导入模块而不导致错误?

最佳答案

据我所知,这可能就是您想要的:

./model/index.ts 看起来像这样:

export * from './resource';
export * from './container';
export * from './entity-type';

然后假设您想从 whatever.service.ts 导入它

whatever.service.ts 看起来像这样:

import {Resource, Container} from '../models';

因为您在模型文件夹中指定了一个索引文件。您应该能够只导入文件夹。如上所述。

希望这更清楚。

关于typescript - 如何从 Angular2 TypeScript 中的模块文件夹加载多个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37751672/

相关文章:

javascript - @angular-material-components/datetime-picker - '@angular/material/datepicker"' 没有导出成员 'DateRange'

javascript - React Native 中未定义不是对象 '_this3.props.navigation()' 错误

magento - 无效的配置字段后端模型: adminhtml/system_config_backend_web_secure_offloaderheader

joomla - joomla模块中的文件上传

javascript - Ionic 2 Angular 2 - 如何通过 Action Sheet 刷新当前页面的数据?

javascript - 在 Typescript 中输入可选的可调用装饰器

javascript - Mongoose 索引html文本?

linux - 将模块.C添加到linux内核

javascript - 从java Controller 返回值

css - Angular 响应式表单模板