Javascript 角形2 DI罐';t解析参数,HTTP客户端模块
我正在学习使用Angular 2,已经达到了一个点,我被下面的错误卡住了。这仅在添加httpClientModule并尝试从组件ngInit发出http get请求后出现。该应用程序正在使用angular 4.3.4Javascript 角形2 DI罐';t解析参数,HTTP客户端模块,javascript,angular,angular-components,angular-httpclient,Javascript,Angular,Angular Components,Angular Httpclient,我正在学习使用Angular 2,已经达到了一个点,我被下面的错误卡住了。这仅在添加httpClientModule并尝试从组件ngInit发出http get请求后出现。该应用程序正在使用angular 4.3.4 Can't resolve all parameters for BlogComponent: (?). app.module.ts的内容 import { NgModule } from '@angular/core'; import { BrowserModule }
Can't resolve all parameters for BlogComponent: (?).
app.module.ts的内容
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
import { BlogComponent } from './blog.component';
import { HomeComponent } from './home.component';
import { appRoutes } from './routes'
@NgModule({
imports: [
BrowserModule,
HttpClientModule,
RouterModule.forRoot(appRoutes)
],
declarations: [
AppComponent,
BlogComponent,
HomeComponent
],
bootstrap: [AppComponent],
})
export class AppModule { }
引发错误的博客组件ts文件的内容
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'test-blog',
template: '<h1>Blog page :)</h1>',
})
export class BlogComponent implements OnInit{
results: string[];
constructor(private http: HttpClient) {}
ngOnInit(): void {
// Make the HTTP request:
this.http.get('/api/blog/all').subscribe(data => {
// Read the result field from the JSON response.
this.results = data['results'];
});
}
}
从'@angular/core'导入{Component,OnInit};
从'@angular/common/http'导入{HttpClient};
@组成部分({
选择器:“测试博客”,
模板:“博客页面:)”,
})
导出类BlogComponent实现OnInit{
结果:字符串[];
构造函数(私有http:HttpClient){}
ngOnInit():void{
//发出HTTP请求:
this.http.get('/api/blog/all').subscribe(数据=>{
//从JSON响应中读取结果字段。
this.results=数据['results'];
});
}
}
尝试使用Http
导入,而不是HttpClient
:
import { Http } from '@angular/http';
...
constructor(private http: Http) {}
尝试使用
Http
导入,而不是HttpClient
:
import { Http } from '@angular/http';
...
constructor(private http: Http) {}
根据这一点,您的组件可能缺少@Injectable()
请试试这个:
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'test-blog',
template: '<h1>Blog page :)</h1>',
})
@Injectable()
export class BlogComponent implements OnInit{
results: string[];
constructor(private http: HttpClient) {}
ngOnInit(): void {
// Make the HTTP request:
this.http.get('/api/blog/all').subscribe(data => {
// Read the result field from the JSON response.
this.results = data['results'];
});
}
}
从'@angular/core'导入{Component,OnInit};
从'@angular/common/http'导入{HttpClient};
@组成部分({
选择器:“测试博客”,
模板:“博客页面:)”,
})
@可注射()
导出类BlogComponent实现OnInit{
结果:字符串[];
构造函数(私有http:HttpClient){}
ngOnInit():void{
//发出HTTP请求:
this.http.get('/api/blog/all').subscribe(数据=>{
//从JSON响应中读取结果字段。
this.results=数据['results'];
});
}
}
根据这一点,您的组件可能缺少@Injectable()
请试试这个:
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'test-blog',
template: '<h1>Blog page :)</h1>',
})
@Injectable()
export class BlogComponent implements OnInit{
results: string[];
constructor(private http: HttpClient) {}
ngOnInit(): void {
// Make the HTTP request:
this.http.get('/api/blog/all').subscribe(data => {
// Read the result field from the JSON response.
this.results = data['results'];
});
}
}
从'@angular/core'导入{Component,OnInit};
从'@angular/common/http'导入{HttpClient};
@组成部分({
选择器:“测试博客”,
模板:“博客页面:)”,
})
@可注射()
导出类BlogComponent实现OnInit{
结果:字符串[];
构造函数(私有http:HttpClient){}
ngOnInit():void{
//发出HTTP请求:
this.http.get('/api/blog/all').subscribe(数据=>{
//从JSON响应中读取结果字段。
this.results=数据['results'];
});
}
}
这里没有明显的错误,你能给出一个关于Plunker的例子吗?是的,如果没有明显的错误,我会考虑创建Plunker。这件事我已经被难倒很久了。@Andrew我也面临着同样的问题,这件事运气好吗?这件事没有明显的问题,你能给我一个关于Plunker的建议吗?是的,如果没有明显的问题,我会考虑创建Plunker。这件事我已经被难倒很久了。@Andrew我也面临着同样的问题,这件事运气好吗?HttpClient
类是有效的,这取决于Angular的版本4.3只是HttpI起初使用Http,但我遇到了其他错误,然后我找到了上面的文档,我使用的是4.3.4,所以看起来HttpClient就是要使用的。HttpClient的HttpClient
类是有效的,这取决于Angular的版本4.3只是HttpI最初使用Http,但我遇到了其他错误,然后我找到了上面的文档,我使用的是4.3.4,所以看起来HttpClient就是要使用的。您已经使用了组件装饰器(@Component
)。您已经使用了组件装饰器(@组件
)。