我刚刚开始黑客的角度2,现在我试图开发我的第一个‘现实世界’应用程序。它的目的是从检索数据,因此我编写了以下代码:
boot.ts:
import {bootstrap} from 'angular2/platform/browser'
import {AppComponent} from './component/app.component'
import {Http, Headers} from 'angular2/http';
bootstrap(AppComponent);app.component.ts:
import {Component} from 'angular2/core';
import {AuthComponent} from './auth.component';
@Component({
selector: 'my-app',
templateUrl: 'app/template/my-app.html',
styleUrls: ['app/style/my-app.css'],
directives: [AuthComponent]
})
export class AppComponent {
}auth.service.ts:
import {Injectable} from 'angular2/core';
import {Http, Headers, HTTP_PROVIDERS} from 'angular2/http';
import {toPromise} from 'rxjs/operator/toPromise';
import {Credentials} from '../model/credentials';
@Injectable()
export class AuthService {
headers : Headers;
http: Http;
constructor(headers: Headers, http: Http){
console.log('CHAMOU CONSTRUTOR SERVICE');
this.headers = headers;
this.http = http;
this.headers.append('Content-Type', 'application/json');
}
}index.html:
<html>
<head>
<title>MegaSíndico</title>
<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.5/js/materialize.min.js"></script>
<!-- 1. Load libraries -->
<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>我的控制台上有一个奇怪的语法错误:
Uncaught SyntaxError: Unexpected token <
Evaluating http://localhost:3000/angular2/http
Error loading http://localhost:3000/app/boot.js这真的很奇怪,因为当我删除服务层的构造函数时,它会工作,我的html会在浏览器上呈现。
发布于 2016-01-09 04:13:59
根据所提供的信息,您使用的任何模块加载程序都试图加载http://localhost:3000/angular2/http,而且您的Web服务器可能正在返回带有HTML主体的404,模块加载程序试图将其计算为JavaScript,但失败了。如果您查看浏览器的“网络”选项卡,就可以确认情况是这样的。
由于您还没有说明要使用哪个模块加载器,所以不可能说明需要做什么才能正确配置它,但基本上:您的模块加载程序没有正确配置,也没有对导入应用正确的路径/文件扩展名映射。它需要(至少)向导入的模块路径添加一个.js,以便从服务器上正确的URL加载。
https://stackoverflow.com/questions/34689075
复制相似问题