简体   繁体   English

angular2:如何解决错误:(SystemJS)模块未定义?

[英]angular2: how to resolve Error: (SystemJS) module is not defined?

I am a new learner of angular2. 我是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. 我们知道在angular2中,有一种通过相对路径引用文件的方法,该路径通过在组件元数据中定义moduleId : module.id来实现。 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 我在这里构建了一个插件: 一个简单的angular2应用

The file structure is as below, very simple: 文件结构如下,非常简单:

在此处输入图片说明

Content of each part is as below: 各部分内容如下:

config.js: 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: 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. 我在module: "commonjs"设置了module: "commonjs" Is there anything wrong with my codes? 我的代码有什么问题吗?

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

It's work for me Angular@4.xx, Typescript@2.xx, SystemJS@0.20.12 它对我有用Angular@4.xx,Typescript@2.xx,SystemJS@0.20.12

declare var __moduleName: string;
import { Component } from '@angular/core';

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

}

If you want to use module.id you can install the @types/node package as a dev dependency. 如果要使用module.id,可以将@ types / node软件包安装为dev依赖项。

This will recognise module as a type. 这会将模块识别为类型。

https://www.npmjs.com/package/@types/node https://www.npmjs.com/package/@types/node

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

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