angular2:如何解决错误:(SystemJS)模块未定义? [英] angular2: how to resolve Error: (SystemJS) module is not defined?

查看:64
本文介绍了angular2:如何解决错误:(SystemJS)模块未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是angular2的新手.我们知道在angular2中,有一种通过相对路径引用文件的方法,该路径通过在组件元数据中定义 moduleId:module.id 来实现.但是,我尝试通过这种方式始终出现以下错误:

 错误:(SystemJS)模块未定义 

我在这里构建了一个插件:

每个部分的内容如下:

config.js:

  System.config({//使用打字稿进行编译transpiler:'打字稿',//打字稿编译器选项typescriptOptions:{glowDecoratorMetadata:是的,模块:"commonjs"},中继:{'打字稿':{出口":"ts"}},路径:{'npm:':'https://unpkg.com/'},//map告诉系统加载器在哪里寻找东西地图: {'app':'./app','@ angular/core':'npm:@ angular/core/bundles/core.umd.js','@ angular/common':'npm:@ angular/common/bundles/common.umd.js','@ angular/compiler':'npm:@ angular/compiler/bundles/compiler.umd.js','@ angular/platform-b​​rowser':'npm:@ angular/platform-b​​rowser/bundles/platform-b​​rowser.umd.js','@ angular/platform-b​​rowser-dynamic':'npm:@ angular/platform-b​​rowser-dynamic/bundles/platform-b​​rowser-dynamic.umd.js','@ angular/http':'npm:@ angular/http/bundles/http.umd.js','@ angular/router':'npm:@ angular/router/bundles/router.umd.js','@ angular/forms':'npm:@ angular/forms/bundles/forms.umd.js','@ angular/core/testing':'npm:@ angular/core/bundles/core-testing.umd.js','@ angular/common/testing':'npm:@ angular/common/bundles/common-testing.umd.js','@ angular/compiler/testing':'npm:@ angular/compiler/bundles/compiler-testing.umd.js','@ angular/platform-b​​rowser/testing':'npm:@ angular/platform-b​​rowser/bundles/platform-b​​rowser-testing.umd.js','@ angular/platform-b​​rowser-dynamic/testing':'npm:@ angular/platform-b​​rowser-dynamic/bundles/platform-b​​rowser-dynamic-testing.umd.js','@ angular/http/testing':'npm:@ angular/http/bundles/http-testing.umd.js','@ angular/router/testing':'npm:@ angular/router/bundles/router-testing.umd.js','rxjs':'npm:rxjs','typescript':'npm:typescript/lib/typescript.js'},//packages定义我们的应用程序包套餐:{应用程序: {main:'main.ts',defaultExtension:'ts'},rxjs:{defaultExtension:'js'}}}); 

app.component.ts:

 从'@ angular/core'导入{组件};@零件({moduleId:module.id,选择器:"app",templateUrl:"app.component.html"})导出类AppComponent {} 

有关其他文件,您可以参考插头.我已经在typescriptOptions中设置了 module:"commonjs" .我的代码有什么问题吗?

解决方案

对于systemjs,在这里我应该使用 __ moduleName 而不是 module.id

I am a new learner of angular2. We know in angular2 there is a way to reference a file by relative path which by defining moduleId : module.id in component meta data. However I tried to to by that way and always get the following error:

Error: (SystemJS) module is not defined 

I have built a plunker here: a simple angular2 app

The file structure is as below, very simple:

Content of each part is as below:

config.js:

System.config({
    //use typescript for compilation
    transpiler: 'typescript',
    //typescript compiler options
    typescriptOptions: {
        emitDecoratorMetadata: true,
        module: "commonjs"
    },
    meta: {
        'typescript': {
            "exports": "ts"
        }
    },
    paths: {
        'npm:': 'https://unpkg.com/'
    },
    //map tells the System loader where to look for things
    map: {

        'app': './app',

        '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
        '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
        '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
        '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
        '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
        '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
        '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
        '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

        '@angular/core/testing': 'npm:@angular/core/bundles/core-testing.umd.js',
        '@angular/common/testing': 'npm:@angular/common/bundles/common-testing.umd.js',
        '@angular/compiler/testing': 'npm:@angular/compiler/bundles/compiler-testing.umd.js',
        '@angular/platform-browser/testing': 'npm:@angular/platform-browser/bundles/platform-browser-testing.umd.js',
        '@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js',
        '@angular/http/testing': 'npm:@angular/http/bundles/http-testing.umd.js',
        '@angular/router/testing': 'npm:@angular/router/bundles/router-testing.umd.js',

        'rxjs': 'npm:rxjs',
        'typescript': 'npm:typescript/lib/typescript.js'
    },
    //packages defines our app package
    packages: {
        app: {
            main: 'main.ts',
            defaultExtension: 'ts'
        },

        rxjs: {
            defaultExtension: 'js'
        }
    }
});

app.component.ts:

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

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

}

For other file, you can refer to the plunker. I have set module: "commonjs" in typescriptOptions. Is there anything wrong with my codes?

解决方案

For systemjs, here I should use __moduleName instead of module.id

这篇关于angular2:如何解决错误:(SystemJS)模块未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆