Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了typescript – Angular 2:ReferenceError:require未定义(…),angular2-polyfills大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
编辑:

我还没弄清楚为什么它不起作用,但我从头开始并获得了working version.似乎没有必要将system.src.js放在角度脚本标签之前,按照下面的答案之一调整.查看工作版本和Angular 2 tutorial.

原始问题:

我正在努力学习Angular 2和Express.我一般都遵循this指南.我收到以下错误

ReferenceError: require is not defined(…) angular2-polyfills.js:1243 
Zone.run @ angular2-polyfills.js:1243
zoneBoundFn @ angular2-polyfills.js:1220
lib$es6$promise$$internal$$tryCatch @ angular2-polyfills.js:468
lib$es6$promise$$internal$$invokeCallback @ angular2-polyfills.js:480
lib$es6$promise$$internal$$publish @ angular2-polyfills.js:451
lib$es6$promise$$internal$$publishRejection @ angular2-polyfills.js:401
(anonymous function) @ angular2-polyfills.js:123
Zone.run @ angular2-polyfills.js:1243
zoneBoundFn @ angular2-polyfills.js:1220
lib$es6$promise$asap$$flush @ angular2-polyfills.js:262

编辑:here是GitHub上项目的破碎版本.

我的项目结构如下:

application
|__client
   |__app
      |__main.ts
      |__main.js
      |__tsconfig.json
   |__typings/ (...)
   |__index.html
   |__typings.json
|__server
   |__server.ts
   |__server.js
   |__tsconfig.json
   |__typings.json
|__node_modules/ (...)

文件server.ts是这样的:

import * as express from "express";
let path = require("path");

let app = express();

let PORT = 8080;

app.use("/node_modules",express.static(__dirname + "/../node_modules"));
app.use("/app",express.static(__dirname + "/../client/app"));

app.get("/",(req,res) => {
  res.sendFile(path.resolve(__dirname,"..","client","index.html"));
});

app.listen(PORT,() => { console.log("Listening on port " + PORT)});

main.ts文件是这样的:

import {Component} from "angular2/core";
import {bootstrap} from "angular2/platform/browser";

@Component({
  selector: "app",template: "<h1>Hello world</h1>"
})
export class AppComponent {}

bootstrap(AppComponent);

index.html文件是这样的:

<!DOCTYPE html>
<html>

<head>
  <Meta charset="utf-8">
  <title></title>

  <script src="node_modules/systemjs/dist/system.src.js"></script>
  <script src="node_modules/es6-shim/es6-shim.min.js"></script>
  <script src="node_modules/systemjs/dist/system-polyfills.js"></script>

  <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>
  <script>
    System.config({
      packages: {
        app: {
          format: "register",defaultExtension: "js"
        }
      }
    });
    System.import("app/main")
      .then(null,console.error.bind(console));
  </script>
</head>

<body>
  <app></app>
</body>

</html>

tsconfig.json文件都是这样的:

{
    "compilerOptions": {
        "target": "es5","module": "system","moduleResolution": "node","isolatedModules": false,"jsx": "react","experimentalDecorators": true,"emitDecoratorMetadata": true,"declaration": false,"noImplicitAny": true,"removeComments": false,"noLib": false,"preserveConstEnums": true,"suppressImplicitAnyIndexErrors": true
    },"exclude": [
        "node_modules","typings/browser","typings/browser.d.ts"
    ],"compileOnSave": true,"buildOnSave": false,"atom": {
        "rewriteTsconfig": false
    }
}

谢谢.

解决方法

angular2-polyfills.js是一个SystemJs包,所以它需要已经安装了SystemJs.

消息需要的是未定义的意思,因为require是systemjs用来导入模块的函数.要解决此问题,请将system.src.js移到顶部,并使其成为页面的第一个脚本:

<script src="node_modules/systemjs/dist/system.src.js"></script>
... other script tags

大佬总结

以上是大佬教程为你收集整理的typescript – Angular 2:ReferenceError:require未定义(…),angular2-polyfills全部内容,希望文章能够帮你解决typescript – Angular 2:ReferenceError:require未定义(…),angular2-polyfills所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: