2017-10-13 121 views
0

試圖開始開發角度2應用程序。在YouTube上找到一個視頻並與之一起運行。它工作得很好,但只在Chrome中。當我運行在IE 11的同一個應用程序,我得到以下錯誤:Angular 2應用程序在Chrome中運行,但不是IE 11

Error: (SystemJS) Syntax error 
SyntaxError: Syntax error 
at Anonymous function (eval code:4:1) 
at Anonymous function (eval code:1:31) 
Evaluating http://localhost:52688/app/app.module.js 
Evaluating http://localhost:52688/app/main.js 
Error loading http://localhost:52688/app/main.js 

所以我去谷歌找到一個解決方案。我瞭解到IE 11不支持es6,所以我需要將我的tsconfig.json中的目標框架從es6更改爲es5。很明顯,我的應用程序並不高興,它會沿着以下方向返回各種錯誤:

"Build:Cannot find name 'Map'" and "Build:Cannot find name 'Set'". 

再次,關閉谷歌。我瞭解到,這兩個對象現在完全不同於es5和es6,許多人提出了一些墊片。因此,我在我的package.json中添加了一個墊片,恢復軟件包,並在我的主頁面中引用它。我嘗試再次建設,但我沒有成功。

我質疑這些墊片是否確實在工作,但我不知道如何讓他們「工作」。謝謝你的幫助。這是所有相關的代碼。如果你需要更多,請讓我知道。

的package.json

{"name": "angular2withvs2015", 
"version": "1.0.0", 
"dependencies": { 
"@angular/common": "~2.1.1", 
"@angular/compiler": "~2.1.1", 
"@angular/core": "~2.1.1", 
"@angular/forms": "~2.1.1", 
"@angular/http": "~2.1.1", 
"@angular/platform-browser": "~2.1.1", 
"@angular/platform-browser-dynamic": "~2.1.1", 
"@angular/router": "~3.1.1", 
"@angular/upgrade": "~2.1.1", 
"bootstrap": "3.3.7", 
"core-js": "^2.4.1", 
"reflect-metadata": "^0.1.8", 
"rxjs": "5.4.3", 
"systemjs": "0.19.39", 
"zone.js": "^0.6.25", 
"es6-shim": "0.35.3"}, 
"devDependencies": { 
"@types/core-js": "^0.9.41", 
"@types/node": "6.0.40", 
"gulp": "3.9.1", 
"gulp-tsc": "^1.2.5", 
"gulp-typescript": "^3.1.2", 
"rimraf": "^2.5.4", 
"typescript": "^2.0.7"}} 

tsconfig.json

{ 
    "compilerOptions": { 
    "target": "es6", 
    "module": "commonjs", 
    "moduleResolution": "node", 
    "sourceMap": true, 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "removeComments": false, 
    "noImplicitAny": true, 
    "types": [] 
    }, 
    "exclude": [ 
    "node_modules" 
    ] 
} 

app.component.ts

import { Component,OnInit } from '@angular/core'; 
@Component({ 
    selector: 'my-app', 
    template: `<h1>Hello {{name}}</h1>`, 
    providers:[GetDataService] 
}) 
export class AppComponent { 
    name = 'Angular 2 (From component)'; 
} 

system.config.js

(function (global) { 
var map = { 
    'app': '/app', 
    '@angular': '/node_modules/@angular', 
    'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api', 
    'rxjs': '/node_modules/rxjs' 

}, 
    packages = { 
     'app': { main: './main.js', defaultExtension: 'js' }, 
     'angular2-in-memory-web-api': { main: './index.js', defaultExtension: 'js' }, 
     'rxjs': { defaultExtension: 'js' } 
    }, 
    ngPackageNames = [ 
     'common', 
     'compiler', 
     'core', 
     'http', 
     'platform-browser', 
     'platform-browser-dynamic', 
     'router', 
     'forms' 
    ]; 

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

ngPackageNames.forEach(packUmd); 


var config = { 
    map: map, 
    packages: packages 
}; 

System.config(config);})(this); 

Default.aspx的

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Angular2Demo10.Default" %> 
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server">  
<script src="node_modules/es6-shim/es6-shim.js"></script> 
<script src="node_modules/core-js/client/shim.js"></script> 

<script src="node_modules/zone.js/dist/zone.js"></script> 
<script src="node_modules/systemjs/dist/system.src.js"></script> 
<script src="system.config.js"></script> 
<script> 
    System.import('app').then(null, console.error.bind(console)); 
</script> 
</head> 
<body> 
    <h1>Hello World (Default.aspx)</h1> 
    <my-app>Loading...</my-app> 
</body></html> 
+0

https://stackoverflow.com/questions/33332394/angular-2-typescript-cant-find-names –

+0

我不知道這是什麼意思。你能簡化它嗎?我假設我需要改變我的package.json中的東西,但我不確定是什麼。 – champ8686

+0

有一堆解決方案。只需花一些時間,嘗試其中的一些......您可以在主文件中添加'/// ',或者嘗試爲' @ types/es6-shim' in your package.json' –

回答

1

改變了我的package.json到:

{"compilerOptions": { 
"lib": [ "es5", "dom", "es6", "dom.iterable", "scripthost" ], 
"noLib": false, 
"target": "es5", 
"module": "commonjs", 
"moduleResolution": "node", 
"sourceMap": true, 
"emitDecoratorMetadata": true, 
"experimentalDecorators": true, 
"removeComments": false, 
"noImplicitAny": true, 
"types": ["node"]}, 
"exclude": [ 
"node_modules" 
] 

此外,一定要這兩個參考加入到您的index.html。

<script src="node_modules/es5-shim/es5-shim.js"></script> 
<script src="node_modules/es6-shim/es6-shim.js"></script> 

來源:https://groups.google.com/forum/#!topic/angular/rR4GTRMUNsw --Steven盧克 Angular 2 typescript can't find names --Jiayi胡

相關問題