Angular2 系统问题
Angular2 systemjs issues
我有这个简单的 TypeScript Angular 2 个例子:
test.ts
import {Component} from 'angular2/core';
@Component({
selector: 'my-app',
template: '<div>Test</div>'
})
export class TestComponent {}
main.ts
import {bootstrap} from 'angular2/platform/browser'
import {TestComponent} from './components/test.component/test'
bootstrap(TestComponent);
index.html
<html>
<head>
<title>This is an Angular 2 test</title>
<!-- Angular dependencies -->
<script src="/node_modules/es6-shim/es6-shim.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>
<!-- App -->
<script>
System.config({
packages: {
'/': {
format: 'register',
defaultExtension: 'js'
}
}
});
System.import('main').then(null, console.error.bind(console));
</script>
</head>
<body>
<my-app></my-app>
</body>
</html>
最后,当我 运行 'tsc' 和我的部署脚本时,node_modules/angular2、node_modules/systemjs、node_modules/rxjs 和 node_modules/es6-shim 文件夹被复制连同编译的 JavaScipt 文件。然后,当我尝试打开 index.html 时,我得到的是:
Uncaught ReferenceError: require is not defined(anonymous function) @ browser.ts:1
angular2-polyfills.js:138 Error: http://localhost:8080/node_modules/angular2/platform/browser.js did not call System.register or AMD define. If loading a global module configure the global name via the meta exports property for script injection support.(…)run @ angular2-polyfills.js:138zoneBoundFn @ angular2-polyfills.js:111lib$es6$promise$$internal$$tryCatch @ angular2-polyfills.js:1511lib$es6$promise$$internal$$invokeCallback @ angular2-polyfills.js:1523lib$es6$promise$$internal$$publish @ angular2-polyfills.js:1494lib$es6$promise$$internal$$publishRejection @ angular2-polyfills.js:1444(anonymous function) @ angular2-polyfills.js:243run @ angular2-polyfills.js:138zoneBoundFn @ angular2-polyfills.js:111lib$es6$promise$asap$$flush @ angular2-polyfills.js:1305
core.ts:6 Uncaught ReferenceError: require is not defined
有人可以帮我解决这个问题吗?我似乎在做与 Angular2 5 分钟快速入门 (https://angular.io/guide/quickstart) 差不多的事情,它只是拒绝为我工作。
感觉我还需要将依赖库添加到 systemjs,但我不确定这样做的正确方法是什么以及 angular2 人员如何让他们的 quistart 演示与简单的 <script></script>
标签。
tsconfig.json
{
"compilerOptions": {
"target": "ES5",
"module": "system",
"moduleResolution" : "node",
"sourceMap": true,
"emitDecoratorMetadata": false,
"experimentalDecorators": false,
"removeComments": true,
"noImplicitAny": true,
"noEmitOnError": true,
"outDir": "build"
},
"exclude": [
"build",
"bundle",
"node_modules",
"public",
"scss",
"html",
"templates",
".sass-cache"
]
}
我认为问题出在您的 SystemJS 配置级别:
System.config({
packages: {
'/': { <----------------
format: 'register',
defaultExtension: 'js'
}
}
});
您应该使用包含您的 TypeScript 源文件的子文件夹更新它。类似的东西:
System.config({
packages: {
app: {
format: 'register',
defaultExtension: 'js'
}
}
});
希望对你有帮助,
蒂埃里
我有这个简单的 TypeScript Angular 2 个例子:
test.ts
import {Component} from 'angular2/core';
@Component({
selector: 'my-app',
template: '<div>Test</div>'
})
export class TestComponent {}
main.ts
import {bootstrap} from 'angular2/platform/browser'
import {TestComponent} from './components/test.component/test'
bootstrap(TestComponent);
index.html
<html>
<head>
<title>This is an Angular 2 test</title>
<!-- Angular dependencies -->
<script src="/node_modules/es6-shim/es6-shim.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>
<!-- App -->
<script>
System.config({
packages: {
'/': {
format: 'register',
defaultExtension: 'js'
}
}
});
System.import('main').then(null, console.error.bind(console));
</script>
</head>
<body>
<my-app></my-app>
</body>
</html>
最后,当我 运行 'tsc' 和我的部署脚本时,node_modules/angular2、node_modules/systemjs、node_modules/rxjs 和 node_modules/es6-shim 文件夹被复制连同编译的 JavaScipt 文件。然后,当我尝试打开 index.html 时,我得到的是:
Uncaught ReferenceError: require is not defined(anonymous function) @ browser.ts:1
angular2-polyfills.js:138 Error: http://localhost:8080/node_modules/angular2/platform/browser.js did not call System.register or AMD define. If loading a global module configure the global name via the meta exports property for script injection support.(…)run @ angular2-polyfills.js:138zoneBoundFn @ angular2-polyfills.js:111lib$es6$promise$$internal$$tryCatch @ angular2-polyfills.js:1511lib$es6$promise$$internal$$invokeCallback @ angular2-polyfills.js:1523lib$es6$promise$$internal$$publish @ angular2-polyfills.js:1494lib$es6$promise$$internal$$publishRejection @ angular2-polyfills.js:1444(anonymous function) @ angular2-polyfills.js:243run @ angular2-polyfills.js:138zoneBoundFn @ angular2-polyfills.js:111lib$es6$promise$asap$$flush @ angular2-polyfills.js:1305
core.ts:6 Uncaught ReferenceError: require is not defined
有人可以帮我解决这个问题吗?我似乎在做与 Angular2 5 分钟快速入门 (https://angular.io/guide/quickstart) 差不多的事情,它只是拒绝为我工作。
感觉我还需要将依赖库添加到 systemjs,但我不确定这样做的正确方法是什么以及 angular2 人员如何让他们的 quistart 演示与简单的 <script></script>
标签。
tsconfig.json
{
"compilerOptions": {
"target": "ES5",
"module": "system",
"moduleResolution" : "node",
"sourceMap": true,
"emitDecoratorMetadata": false,
"experimentalDecorators": false,
"removeComments": true,
"noImplicitAny": true,
"noEmitOnError": true,
"outDir": "build"
},
"exclude": [
"build",
"bundle",
"node_modules",
"public",
"scss",
"html",
"templates",
".sass-cache"
]
}
我认为问题出在您的 SystemJS 配置级别:
System.config({
packages: {
'/': { <----------------
format: 'register',
defaultExtension: 'js'
}
}
});
您应该使用包含您的 TypeScript 源文件的子文件夹更新它。类似的东西:
System.config({
packages: {
app: {
format: 'register',
defaultExtension: 'js'
}
}
});
希望对你有帮助, 蒂埃里