所以,我正在通过打字稿在他们的网站上关注角度2指南,并且我陷入了http api集成.我正在尝试创建一个简单的应用程序,可以通过soundcloud api搜索一首歌,但是我很难实现和理解如何开始,而且在线资源以很多不同的方式完成它(我相信快速的角度2语法更改)早些时候).
所以目前我的项目看起来像这样
app
components
home
home.component.ts
...
search
search.component.ts
...
app.ts
...
services
soundcloud.ts
bootstrap.ts
index.html
Run Code Online (Sandbox Code Playgroud)
在示例中没有任何花哨的东西,主要文件将是
app.ts
import {Component, View} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
import {HomeComponent} from './home/home.component';
import {SearchComponent} from './search/search.component';
@Component({
selector: 'app',
templateUrl: 'app/components/app.html',
directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
{path: '/home', name: 'Home', component: HomeComponent, useAsDefault: true},
{path: '/search', name: 'Search', component: SearchComponent}
])
export class App { }
Run Code Online (Sandbox Code Playgroud)
bootstrap.ts
import {App} from './components/app';
import {bootstrap} from 'angular2/platform/browser';
import {ROUTER_PROVIDERS} from 'angular2/router'; …Run Code Online (Sandbox Code Playgroud) 因此,在Angular2的RC5版本中,他们弃用HTTP_PROVIDERS并引入了HttpModule.对于我的应用程序代码,这工作正常,但我正在努力在我的Jasmine测试中进行更改.
这是我目前在我的规范中所做的事情,但由于不推荐使用HTTP_PROVIDERS,我现在应该做些什么呢?有什么我需要提供而不是HTTP_PROVIDERS?在RC5世界中这样做的正确方法是什么?
beforeEach(() => {
reflectiveInjector = ReflectiveInjector.resolveAndCreate([
HTTP_PROVIDERS,
...
]);
//other code here...
});
it("should....", () => { ... });
Run Code Online (Sandbox Code Playgroud) 我正在使用Angular 2和TypeScript处理Meteor Web应用程序.对于使用REST API,我使用Swagger Codegen生成了客户端代码.不幸的是,GitHub上没有样本,如何使用其余的客户端.
我有一个角度2视图组件,它注入一个Angular 2服务(ProductTreeService)和生成的API(都标记为"@Injectable"):
@Component({
selector: 'panel-product-selection',
template,
providers: [ProductTreeService, UserApi],
directives: [ProductTreeComponent]
})
export class PanelProductSelectionComponent
{
private categoriesProductsTree: Collections.LinkedList<CategoryTreeElement>;
private productTreeService: ProductTreeService;
private userApi: UserApi;
constructor(productTreeService: ProductTreeService, userApi: UserApi)
{
this.productTreeService = productTreeService;
this.userApi = userApi;
}
ngOnInit(): void
{
//...
}
}
Run Code Online (Sandbox Code Playgroud)
虽然角度服务只是可访问的并且一切正常,但是当我注入UserApi时应用程序崩溃了.UserApi和ProductTreeService之间的唯一区别是构造函数:服务没有构造函数参数,生成的Api类具有:
constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string) {
if (basePath) {
this.basePath = basePath;
}
}
Run Code Online (Sandbox Code Playgroud)
那么我该如何注入生成的API呢?
你好,我得到这样的错误是什么原因?
StaticInjectorError [Http]:StaticInjectorError [Http]:NullInjectorError:没有Http提供程序!在tryResolveToken(core.js:1153)的resolveToken(core.js:1211)的NullInjector.get(core.js:923)处在resolveToken(core.js :)的StaticInjector.get(core.js:1024)输入代码1211)在tryResolveToken(core.js:1153)在StaticInjector.get(core.js:1024)在resolveNgModuleDep(core.js:10585)在NgModuleRef.get(core.js:11806)在resolveDep(core.js:12302) )
import { Injectable } from '@angular/core' import {Todo} from './Todo'
import {Http, Response,Headers,RequestOptions} from '@angular/Http' import {Observable} from 'rxjs/Observable' import 'rxjs/add/operator/do' import 'rxjs/add/operator/catch' import 'rxjs/add/operator/map'
@Injectable() export class TodoService{
constructor(private http: Http){}
todoUrl = "https://jsonplaceholder.typicode.com/todos";
getTodos():Observable{ return this.http.get("https://jsonplaceholder.typicode.com/todos") .map((res:Response)=>res.json()) .do(data=>console.log("TODOS LIST")) } }
Run Code Online (Sandbox Code Playgroud) angular ×4
javascript ×3
angular-http ×1
angular5 ×1
http ×1
meteor ×1
swagger ×1
typescript ×1