angular - 如何让我的 Angular 2 应用程序处理桶文件导入?

标签 angular typescript systemjs

我正在尝试遵循 Angular 2 样式指南 04-10 创建和导入桶,在此处找到:https://angular.io/docs/ts/latest/guide/style-guide.html

当我运行我的应用程序时,angular 现在会尝试加载一个不存在的文件:“my-component-name-here.js”。

为了说明问题,我修改了 Angular 2 示例应用程序以使用桶文件,它现在也抛出 404 错误。

我将 heroes 组件文件放入它们自己的名为 heroes 的文件夹中。我创建了一个名为 heroes/index.ts 的新文件:

export * from './heroes/heroes.component';

我将 app.component.ts 中的导入语句更改为:

import { HeroesComponent } from './heroes';

当应用程序尝试加载时,在开发者控制台中出现以下错误:

Error: Error: XHR error (404 Not Found) loading app/heroes.ts(…)

plunkr 可以在这里找到: http://plnkr.co/edit/YXY0PwuFot1g1s6qTqDt?p=preview

我怀疑这与 SystemJS 有关,但我对它的了解还不足以修复它。谁能帮忙?提前致谢!

最佳答案

你必须做一些改变才能使桶工作,

index.ts

   export * from './heroes.component';

systemjs.config.js

map 添加一个条目,如下所示,

  'heroes':                     'app/heroes', 

然后在 packages 中创建如下条目,

  'heroes': { main: 'index.ts',  defaultExtension: 'ts' },

这将解决 barrel 问题,但不能完全解决您的所有问题,因为您在 heroes 组件中也引用了 hero-detail,因此您必须工作在那些。

关于angular - 如何让我的 Angular 2 应用程序处理桶文件导入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37260119/

相关文章:

javascript - 我如何正确处理依赖于 ViewChild 的组件的变化检测和 Angular 2 中可观察到的数据?

Angular2 生成器函数

javascript - 带有 polymer + 元素的 SystemJS

javascript - 我如何像 Java 包一样组织我的 Angular 应用程序文件夹?

javascript - 如何强制 TypeScript 允许 null 作为索引类型?

typescript - JSPM 和 Typescript 导入

javascript - Angular 2+ HTTP POST 和 GDrive API。带名称的断点续传文件上传

html - 带有 X 右上角的 Angular 8 Material 对话框关闭按钮

javascript - 动态绑定(bind)图像 Angular 8

typescript - 如何从泛型函数中的参数类型推断返回类型?