简体   繁体   English

Angular2:模块未正确导入

[英]Angular2: modules are not imported properly

I'm following the Tour of Heroes tutorial and everything is working fine, except for one thing: When i import a custom module, I have to declare the import with a .js extension, otherwise i get a 404: 我正在遵循《 英雄旅》教程,并且一切正常,除了以下几点:导入自定义模块时,必须以.js扩展名声明导入,否则得到404:

//This works fine
import {Component} from 'angular2/core';

//This gives me a 404 module not found (http://localhost:3000/assets/html/app/hero)
import {HeroDetailComponent } from './hero-detail.component';

//This works fine but the editor (VS Code) don't like it
import {HeroDetailComponent } from './hero-detail.component.js';

Am i missing something? 我想念什么吗?

There's a defaultJSExtensions setting that you may add to the System.config. 您可以将默认的JSExtensions设置添加到System.config中。

From the documentation: 从文档中:

System.defaultJSExtensions = true;

// requests ./some/module.js instead
System.import('./some/module');

You can also set the defaultExtension of packages. 您还可以设置软件包的defaultExtension。

Again, from the documentation: 再次,从文档中:

System.config({
  packages: {
    // meaning [baseURL]/local/package when no other rules are present
    // path is normalized using map and paths configuration
    'local/package': {
      main: 'index.js',
      format: 'cjs',
      defaultExtension: 'js',
      map: {
        // use local jquery for all jquery requires in this package
        'jquery': './vendor/local-jquery.js',

        // import '/local/package/custom-import' should route to '/local/package/local/import/file.js'
        './custom-import': './local/import/file.js'
      },
      meta: {
        // sets meta for modules within the package
        'vendor/*': {
          'format': 'global'
        }
      }
    }
  }
});

Thanks to @JSess who pointed me in the right direction i found out that my configuration was wrong, since the attributes under the package are actually a path to the modules: 感谢@JSess向我指出了正确的方向,我发现我的配置是错误的,因为包下的属性实际上是模块的路径:

System.config({
        packages: {
          //THIS IS THE WRONG PATH
          'app': {
            format: 'register',
            defaultExtension: 'js'
          }
        }
      });

I just needed to change the attribute under packages to reflect the actual path to the modules: 我只需要更改包下的属性即可反映模块的实际路径:

System.config({
        packages: {
          'assets/html/app': {
            format: 'register',
            defaultExtension: 'js'
          }
        }
      });

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

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