gpt4 book ai didi

javascript - Angular2 5 分钟安装错误 - 未定义要求

转载 作者:太空狗 更新时间:2023-10-29 16:50:52 25 4
gpt4 key购买 nike

我正在做 Angular2 5 minute quick start

教程现在大约进行了一半,我已经正确设置了以下文件:

  • index.html,
  • app.component.ts
  • 应用程序/boot.ts
  • package.json
  • tconfig.json

运行 npm start 并收到此错误:


Uncaught ReferenceError: require is not defined(anonymous function) @ boot.js:1
angular2-polyfills.js:143
Uncaught TypeError: Cannot read property 'split' of undefinedreadMemberExpression @ system.src.js:1456(anonymous function) @ system.src.js:3224(anonymous function) @ system.src.js:3749complete @ system.src.js:2487run @ angular2-polyfills.js:138zoneBoundFn @ angular2-polyfills.js:111


我找到了这个 link about using the es6 shim 并且包含了 <script src="node_modules/es6-shim/es6-shim.js"></script>

但是我仍然遇到 Uncaught ReferenceError: require is not defined 错误。


应用程序组件.ts

import {Component} from 'angular2/core';

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

(应用程序/boot.ts)

import {bootstrap}    from 'angular2/platform/browser'
import {AppComponent} from './app.component'

bootstrap(AppComponent);

index.html

<html>

<head>
<title>Angular 2 QuickStart</title>

<!-- 1. Load libraries -->
<script src="node_modules/es6-shim/es6-shim.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>

<!-- 2. Configure SystemJS -->
<script>
System.config({
packages: {
app: {
format: 'register',
defaultExtension: 'js'
}
}
});
System.import('app/boot')
.then(null, console.error.bind(console));
</script>

</head>

<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app>
</body>

</html>

package.json

{
"name": "angular2-quickstart",
"version": "1.0.0",
"scripts": {
"tsc": "tsc",
"tsc:w": "tsc -w",
"lite": "lite-server",
"start": "concurrent \"npm run tsc:w\" \"npm run lite\" "
},
"license": "ISC",
"dependencies": {
"angular2": "2.0.0-beta.0",
"systemjs": "0.19.6",
"es6-promise": "^3.0.2",
"es6-shim": "^0.33.3",
"reflect-metadata": "0.1.2",
"rxjs": "5.0.0-beta.0",
"zone.js": "0.5.10"
},
"devDependencies": {
"concurrently": "^1.0.0",
"lite-server": "^1.3.1",
"typescript": "^1.7.3"
}
}

tconfig.json

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

编译后的app/boot.js

enter image description here

来自 npm start 的最后一个日志

enter image description here

最佳答案

我发现我报错的原因是我的tsconfig.json 文件中的模块设置为“commonjs”,将其更改为system 修复了它,现在它看起来像下面

{
"compilerOptions": {
"target": "es5",
"module": "system",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true
},
"exclude": [
"node_modules"
]
}

编辑这个答案是基于 Angular 2 beta 17 的。根据这个答案的时间和 Angular 2 经历的快速变化,这可能不再有效。

关于javascript - Angular2 5 分钟安装错误 - 未定义要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34730010/

25 4 0
文章推荐: c - 为什么 C 不正确地打印我的十六进制值?
文章推荐: c - Lua table.getn() 返回 0?
文章推荐: char * 作为 C 中的引用
文章推荐: HTML5
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com