Angular 2 Typescript compiler error

2019-07-11 12:41发布

问题:

I think its time to use Angular2 in development, so I want to start, but I have stuck at Error raised by TS compiler. Im using django-pipelines and pipelines-typescript package. It seems that compiler works ok if i create a .ts file it outputs .js and its working. But when I trying to use angular2 it outputs " error TS2304: Cannot find name 'Map'" and "'Promise'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/promise.d.ts(3,14): error TS2304: Cannot find name promise":

    CompilerError at /
b"/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/core/change_detection/parser/locals.d.ts(3,14): error TS2304: Cannot find name
 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/core/change_detection/parser/locals.d.ts(4,42): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/core/linker/element_injector.d.ts(71,32): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/core/linker/element_injector.d.ts(73,17): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/core/linker/element_injector.d.ts(77,184): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/core/linker/element_injector.d.ts(82,182): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/core/linker/element_injector.d.ts(106,37): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/core/linker/proto_view_factory.d.ts(28,146): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/core/linker/view.d.ts(52,144): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/core/linker/view.d.ts(76,79): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/core/linker/view.d.ts(77,73): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/core/linker/view.d.ts(95,31): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/core/linker/view.d.ts(98,18): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/core/linker/view.d.ts(101,24): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/core/linker/view.d.ts(104,162): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/core/linker/view.d.ts(105,160): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/core/render/api.d.ts(317,74): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/core/zone/ng_zone.d.ts(2,37): error TS2304: Cannot find name 'Zone'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/collection.d.ts(1,25): error TS2304: Cannot find name 'MapConstructor'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/collection.d.ts(2,25): error TS2304: Cannot find name 'SetConstructor'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/collection.d.ts(4,27): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/collection.d.ts(4,39): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/collection.d.ts(7,9): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/collection.d.ts(8,30): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/collection.d.ts(11,43): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/collection.d.ts(12,27): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/collection.d.ts(14,23): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/collection.d.ts(15,25): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/collection.d.ts(94,41): error TS2304: Cannot find name 'Set'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/collection.d.ts(95,22): error TS2304: Cannot find name 'Set'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/collection.d.ts(96,25): error TS2304: Cannot find name 'Set'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/lang.d.ts(1,1): error TS1148: Cannot compile modules unless the '--module' flag is provided.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/lang.d.ts(2,22): error TS2304: Cannot find name 'BrowserNodeGlobal'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/lang.d.ts(48,59): error TS2304: Cannot find name 'Map'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/promise.d.ts(1,10): error TS2304: Cannot find name 'Promise'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/promise.d.ts(3,14): error TS2304: Cannot find name 'Promise'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/promise.d.ts(8,32): error TS2304: Cannot find name 'Promise'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/promise.d.ts(9,38): error TS2304: Cannot find name 'Promise'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/promise.d.ts(10,35): error TS2304: Cannot find name 'Promise'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/promise.d.ts(10,93): error TS2304: Cannot find name 'Promise'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/promise.d.ts(11,34): error TS2304: Cannot find name 'Promise'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/promise.d.ts(12,32): error TS2304: Cannot find name 'Promise'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/promise.d.ts(12,149): error TS2304: Cannot find name 'Promise'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/angular2/src/facade/promise.d.ts(13,43): error TS2304: Cannot find name 'Promise'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/js/sceneapp/app.ts(1,1): error TS6053: File '/Users/bbx/envs/dscn_3/bin/scene/scene/static/js/typings/angular2-meteor.d.ts' not found.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/js/sceneapp/app.ts(8,14): error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Specify '--experimentalDecorators' to remove this warning.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/rxjs/CoreOperators.d.ts(22,67): error TS2304: Cannot find name 'Promise'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/rxjs/CoreOperators.d.ts(72,67): error TS2304: Cannot find name 'Promise'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/rxjs/CoreOperators.d.ts(77,31): error TS2304: Cannot find name 'PromiseConstructor'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/rxjs/CoreOperators.d.ts(77,54): error TS2304: Cannot find name 'Promise'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/rxjs/Observable.d.ts(65,67): error TS2304: Cannot find name 'PromiseConstructor'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/rxjs/Observable.d.ts(65,88): error TS2304: Cannot find name 'Promise'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/rxjs/Observable.d.ts(72,84): error TS2304: Cannot find name 'Promise'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/rxjs/Observable.d.ts(77,38): error TS2304: Cannot find name 'Promise'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/rxjs/Observable.d.ts(100,66): error TS2304: Cannot find name 'Promise'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/rxjs/Observable.d.ts(154,66): error TS2304: Cannot find name 'Promise'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/rxjs/Observable.d.ts(159,31): error TS2304: Cannot find name 'PromiseConstructor'.\n/Users/bbx/envs/dscn_3/bin/scene/scene/static/rxjs/Observable.d.ts(159,54): error TS2304: Cannot find name 'Promise'.\n"

My template is:

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link href="/static/css/style.css" rel="stylesheet" type="text/css" media="screen,projection" />
    <script type="text/javascript" src="/static/typescript/lib/tsc.js" charset="utf-8"></script>
<script type="text/javascript" src="/static/es6-shim/es6-shim.js" charset="utf-8"></script>
<script type="text/javascript" src="/static/rxjs/Rx.js" charset="utf-8"></script>
<script type="text/javascript" src="/static/systemjs/dist/system.js" charset="utf-8"></script>
<script type="text/javascript" src="/static/angular2/bundles/angular2.js" charset="utf-8"></script>
<script type="text/javascript" src="/static/js/sceneapp/app.js" charset="utf-8"></script>
    </head>
<body>
Works
</body>
</html>

and app.ts is

import {Component} from 'angular2/core';

@Component({
    selector: 'my-app',
    template: '<h1>My First Angular 2 App</h1>'
})
export class AppComponent {}

So I see that it cannot find a promise and map.

回答1:

These scripts must be included for a basic angular2 application, ES6-shim is not necessary when you include angular2-polyfills:

<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>

After this you can make a systemjs script which will load your app:

System.config({
    packages: {
        'static/js': {
            format: 'register',
            defaultExtension: 'js'
        }
    }
});
System.import('sceneapp/app').then(null, console.error.bind(console));

Make sure to place the bootstrap somewhere as well.

Just follow the angular2 quickstart. That is a good starting point anyways for problems like this