SystemJS不会将资产文件移动到dist文件夹

时间:2018-06-09 06:19:05

标签: angular systemjs

您好我在尝试学习角度而不使用cli来理解内部是如何工作的。我使用systemjs作为模块加载器。我的问题是,systemjs是否假设将.html.css文件与已编译的dist文件一起移动到.ts文件夹?因为如果我想在我的组件中使用相对路径,我需要在@Component装饰器中设置moduleId: module.id。然后,当我加载浏览器时,它会尝试从/dist/app/app.component.html获取这些资产,但这些资产并不存在。

app.component.ts

import { Component } from "@angular/core";

@Component({
    moduleId: module.id,
    selector: "app-root",
    templateUrl: "./app.component.html",
    styleUrls: ["./app.component.css"]
})
export class AppComponent {
    title = "My First Angular App!";
}

ts.config.json

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "noEmitOnError": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "allowJs": true,
        "removeComments": false,
        "noImplicitAny": false,
        "outDir": "dist"
    },
    "include": [
        "src/**/*"
    ],
    "exclude": [
        "node_modules"
    ]
}

systemjs.config.json

/**
 * System configuration for Angular 2 samples
 * Adjust as necessary for your application needs.
 */
(function(global) {

  // map tells the System loader where to look for things
  var map = {
    // our app is within the app folder
    'app': 'dist', // 'dist',

    // angular bundles
    '@angular': 'node_modules/@angular',
    'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',

    // other dependencies
    'rxjs': 'node_modules/rxjs',
    'rxjs-compat': 'node_modules/rxjs-compat',
    'core-js': 'node_modules/core-js',
    'zone.js': 'node_modules/zone.js'
  };

  // packages tells the System loader how to load when no filename and/or no extension
  var packages = {
    'app': {
      main: 'main.js',
      defaultExtension: 'js'
    },
    'rxjs': {
      main: 'index.js',
      defaultExtension: 'js'
    },
    'rxjs/operators': {
      main: 'index.js',
      defaultExtension: 'js'
    },
    'angular2-in-memory-web-api': {
      main: 'index.js',
      defaultExtension: 'js'
    },
    'core-js': {},
    'zone.js': {}
  };

  var ngPackageNames = [
    'common',
    'compiler',
    'core',
    'forms',
    'http',
    'platform-browser',
    'platform-browser-dynamic',
    'router',
    'router-deprecated',
    'upgrade',
  ];

  // Individual files (~300 requests):
  function packIndex(pkgName) {
    packages['@angular/' + pkgName] = {
      main: 'index.js', 
      defaultExtension: 'js'
    };
  }

  // Bundled (~40 requests):
  function packUmd(pkgName) {
    packages['@angular/' + pkgName] = {
      main: 'bundles/' + pkgName + '.umd.js',
      defaultExtension: 'js' };
  }

  // Most environments should use UMD; some (Karma) need the individual index files
  var setPackageConfig = System.packageWithIndex ? packIndex : packUmd;

  // Add package entries for angular packages
  ngPackageNames.forEach(setPackageConfig);

  var config = {
    map: map,
    packages: packages
  };
  System.config(config);
})(this);

如果它有帮助,我正在使用角度版本6。

谢谢!

1 个答案:

答案 0 :(得分:1)

答案是“否”,一些开发人员使用gulp来做到这一点。 我喜欢这样,在systemjs.config.json

map: {
  // our app is within the app folder
  app: 'app',

在app.component.ts中:

@Component({
 selector: 'my-app',
 templateUrl: 'app/app.component.html'

有关更多详细信息,请参见: https://github.com/Longfld/AngularQuickStartForBeginning/blob/master/app/app.component.ts