简体   繁体   English

Angular2:Rollupjs捆绑包含组件类中的相对路径

[英]Angular2: Rollupjs bundle with relative paths in component class

To support i18n as explained here I have to support AOT and therefore need to use relative templateUrl and styleUrls in my components and set moduleId: module.id . 为了支持国际化的解释在这里我要支持AOT,因此需要使用相对templateUrlstyleUrls在我的组件和设置moduleId: module.id

This works perfect for my dev build which does not bundle and loads the files using systemjs (built using gulp) 这适用于我的dev构建,它不使用systemjs捆绑和加载文件(使用gulp构建)

  1. compile typescript: `target: es5, module: commonjs' 编译打字稿:`target:es5,module:commonjs'

In the production build I use rollup.js to tree-shake and bundle. 在生产构建中,我使用rollup.js进行树抖动和捆绑。 Here I have to compliation steps since rollup needs es2015 modules and the browser (and to have module.id set in the components) needs common.js 在这里我必须补充步骤,因为汇总需要es2015模块和浏览器(以及在组件中设置module.id)需要common.js

  1. compile typescript: target: es5, module: es2015 编译target: es5, module: es2015target: es5, module: es2015
  2. bundle with rollup.js: format: iife 与rollup.js捆绑: format: iife
  3. compile typescript again: target: es5, module: commonjs, allowJs: true 再次编译打字稿: target: es5, module: commonjs, allowJs: true

Doing that I get the following error when running the app: failed to load app.component.html 这样做我在运行应用程序时遇到以下错误: 无法加载app.component.html

Version info: 版本信息:

  • angular: 2.2.1 角度:2.2.1
  • typescript: 2.0.10 打字稿:2.0.10
  • rollup: 0.36.3 汇总:0.36.3

Does anyone have an idea what is causing the problem? 有谁知道导致问题的原因是什么? Thanks a lot for your help 非常感谢你的帮助

when you use module.id you have to declare a variable on the component for modules. 当您使用module.id时,您必须在模块的组件上声明一个变量。 that is: 那是:

declare var module: any;
import { Component } from '@angular/core';

@Component({
  moduleId: module.id,
  selector: 'app',
  templateUrl: 'content.component.html'
})
export class ContentComponent {}

I hope this works for you 我希望这适合你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM