【问题标题】:Angular 2 Application runs in Chrome but not IE 11Angular 2 应用程序在 Chrome 但不是 IE 11 中运行
【发布时间】:2018-03-26 00:36:17
【问题描述】:

尝试开始开发 Angular 2 应用程序。在 YouTube 上找到一个视频并运行它。它工作得很好,但只在 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 之间完全不同,许多人建议使用一些 shim。所以我在我的 package.json 中添加了一个 shim,恢复包,并在我的主页中引用它。我再次尝试构建,但没有成功。

我质疑这些垫片是否真的在工作,但我不确定如何让它们“工作”。谢谢你的帮助。这是所有相关的代码。如果您需要更多,请告诉我。

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);

默认.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>

【问题讨论】:

  • 我不知道这意味着什么。你能把它简化吗?我假设我需要更改 package.json 中的内容,但我不确定是什么。
  • 有很多解决方案。只需花一些时间尝试其中的几个...您可以将///&lt;reference path="node_modules/angular2/typings/browser.d.ts"/&gt; 添加到主文件中,或者尝试在您的package.json 中添加@types/es6-shim 的依赖项
  • 按照胡佳一的回答搞定了!感谢蒂姆的参考!

标签: angular internet-explorer-11 npm-install package.json


【解决方案1】:

将我的 tsconfig.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 ——史蒂文·卢克 Angular and Typescript: Can't find names --胡佳一

【讨论】:

  • 你不是说你的 tsconfig.json 吗?
猜你喜欢
  • 1970-01-01
  • 2020-06-05
  • 2018-12-14
  • 1970-01-01
  • 1970-01-01
  • 2014-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多