Cos*_*sis 24 beta npm typescript angular
从5分钟的快速启动开始,我一直在玩angular2 beta,遇到了一个让我难过的问题.
这是一个愚蠢的版本,显示我遇到的问题.首先,这是一个完美的hello world应用程序.
的package.json
{
"name": "...",
"version": "0.0.1",
"description": "...",
"author": {
"name": "...",
"email": "..."
},
"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",
"bootstrap": "^3.3.6",
"es6-promise": "^3.0.2",
"es6-shim": "^0.33.3",
"reflect-metadata": "^0.1.2",
"rxjs": "5.0.0-beta.0",
"systemjs": "0.19.6",
"zone.js": "^0.5.10"
},
"devDependencies": {
"concurrently": "^1.0.0",
"lite-server": "^1.3.1",
"typescript": "^1.7.3"
}
}
Run Code Online (Sandbox Code Playgroud)
的index.html
<head>
<title>Title</title>
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
<link href="styles.css" rel="stylesheet" />
<!-- 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></my-app>
</body>
Run Code Online (Sandbox Code Playgroud)
应用程序/ boot.ts
import {bootstrap} from 'angular2/platform/browser'
import {AppComponent} from './app.component'
import {AccountService} from './accounts/account.service'
bootstrap(AppComponent);
Run Code Online (Sandbox Code Playgroud)
应用程序/ app.component.ts
import {Component, View} from 'angular2/core';
import {RegisterFormComponent} from './accounts/register-form.component'
@Component({
selector: 'my-app',
})
@View({
template: 'hello world',
})
export class AppComponent {
}
Run Code Online (Sandbox Code Playgroud)
我实际上想要调用我的Web Api服务,所以我试图关注Http的文档,我更新boot.ts如下:
新app/boot.ts
import {bootstrap} from 'angular2/platform/browser'
import {AppComponent} from './app.component'
import {AccountService} from './accounts/account.service'
import {HTTP_PROVIDERS} from 'angular2/http';
bootstrap(AppComponent, [HTTP_PROVIDERS]);
Run Code Online (Sandbox Code Playgroud)
这就是事情窒息的地方.
Chrome给了我:
uncaught SyntaxError: Unexpected token < - http:1
uncaught SyntaxError: Unexpected token < - angular2-polyfills.js:138
evaluating http://localhost:3000/angular2/http
error loading http://localhost:3000/app/boot.js
Run Code Online (Sandbox Code Playgroud)
如果我尝试将HTTP_PROVIDERS设置为我的组件上的viewProvider,它也会失败.
有没有其他人有运气让Http在angular2 beta中正确注入?
Eri*_*nez 52
当您尝试导入使用SystemJS时未包含在HTML中的内容时,会发生此错误.像Webpack这样的模块捆绑器可以为您处理所有这些.
例如,对于您的情况,您必须添加Http包,它是一个单独的包
<script src="https://code.angularjs.org/2.0.0-beta.0/http.dev.js"></script>
Run Code Online (Sandbox Code Playgroud)
尝试使用路由器时,您会看到同样的错误,而您忘记添加路由器捆绑包.
从@ pkozlowski-opensource的回购中检查这两种配置之间的区别
bundle.js
为您捆绑该文件中的所有内容.很高兴它有所帮助.
E. *_*tes 11
如果您使用的是npm,请在脚本标记中包含对本地安装的http引用:
<script src="node_modules/angular2/bundles/http.dev.js"></script>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9101 次 |
最近记录: |