我刚刚创建了样例应用程序,如jhon 的示例.(快速启动程序)。
import { Injectable } from '@angular/core';
import { Http, Response , Headers} from '@angular/http';
import 'rxjs/add/operator/toPromise';
import { Hero } from './hero';
import { Code } from './code';
import { HEROES } from './mock-heroes';
@Injectable()
export class HeroService {
constructor(private http: Http) { }
private heroesUrl = 'http://localhost:8081//Myserver/rest/heros'; // URL to web api
getHeroes() {
return this.http.get(this.heroesUrl)
.toPromise()
.then(
response => response.json() as Hero[])
.catch(this.handleError);
}
getHero(id: number) {
return this.getHeroes()
.then(heroes => heroes.find(hero => hero.id === id));
}
private handleError(error: any) {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
}服务类运行良好,UI中也列出了英雄(只需在这里忘记CROS或fine问题)。但是我注意到一件事,对http://localhost:8081//Myserver/rest/heros的其余调用是直接从浏览器发送的(我可以在Developer工具的网络选项卡中看到它)。
根据我的应用程序,它不应该从浏览器发送,而是应该像JSF中的后端bean类一样处理,然后向相关组件提供数据。我相信这也将解决我的CROS或this问题。Angular2有这样的选择吗?请提出实现这一目标的正确方法。
发布于 2016-08-24 07:00:52
您可以使用WebWorkers将代码执行移出主UI线程。Angular2对此提供了直接支持(实验性)。
我怀疑这在CORS方面会有什么改变。要使CORS工作,您的服务器需要使用正确的CORS头进行响应。
另见http://www.syntaxsuccess.com/viewarticle/web-workers-in-angular-2.0
https://stackoverflow.com/questions/39116420
复制相似问题