无法使用 http 提供程序;模块 'appModule' 导入的预期值 'HttpClient'
unable to use http provider ; expected value 'HttpClient' imported by the module 'appModule'
我正在向 ionic3 和 Angular 传递一个新手。我看了一些 youtube 并阅读了一些 tuto 来做一个简单的应用程序来调用 ajax 服务器。但是当使用 Http 模块时,我得到了一个编译错误:
Error: Unexpected value 'HttpClient' imported by the module 'AppModule'.
Please add a @NgModule annotation.
at syntaxError (http://localhost:8100/build/vendor.js:78094:34)
at http://localhost:8100/build/vendor.js:92717:44
at Array.forEach (<anonymous>)
at CompileMetadataResolver.getNgModuleMetadata
(http://localhost:8100/build/vendor.js:92700:49)
at JitCompiler._loadModules
(http://localhost:8100/build/vendor.js:111170:87)
at JitCompiler._compileModuleAndComponents
(http://localhost:8100/build/vendor.js:111131:36)
at JitCompiler.compileModuleAsync
(http://localhost:8100/build/vendor.js:111047:37)
at CompilerImpl.compileModuleAsync
(http://localhost:8100/build/vendor.js:76930:49)
at PlatformRef.bootstrapModule
(http://localhost:8100/build/vendor.js:5799:25)
at Object.204 (http://localhost:8100/build/main.js:310:109)
此处 git 代码:ionic app
我要使用 http 提供商。但是当我将 class 注入构造函数时,我得到了错误:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/do';
/*
Generated class for the GetDataProvider provider.
See https://angular.io/guide/dependency-injection for more info on providers
and Angular DI.
*/
@Injectable()
export class GetDataProvider {
private url: string = '192.168.0.110/ajax_inputs';
constructor(private http: HttpClient) {
console.log('Hello GetDataProvider Provider');
}
getData(){
return this.http.get(this.url)
.do(res => console.log(res));
}
}
您需要在 app.module.ts 中导入 HttpClientModule 而不是 HttpClient。
如下图
import { HttpClientModule } from '@angular/common/http';
然后将其添加到导入数组中,如下所示。
imports: [
BrowserModule,
IonicModule.forRoot(MyApp),
//HttpModule,
HttpClientModule
],
我正在向 ionic3 和 Angular 传递一个新手。我看了一些 youtube 并阅读了一些 tuto 来做一个简单的应用程序来调用 ajax 服务器。但是当使用 Http 模块时,我得到了一个编译错误:
Error: Unexpected value 'HttpClient' imported by the module 'AppModule'.
Please add a @NgModule annotation.
at syntaxError (http://localhost:8100/build/vendor.js:78094:34)
at http://localhost:8100/build/vendor.js:92717:44
at Array.forEach (<anonymous>)
at CompileMetadataResolver.getNgModuleMetadata
(http://localhost:8100/build/vendor.js:92700:49)
at JitCompiler._loadModules
(http://localhost:8100/build/vendor.js:111170:87)
at JitCompiler._compileModuleAndComponents
(http://localhost:8100/build/vendor.js:111131:36)
at JitCompiler.compileModuleAsync
(http://localhost:8100/build/vendor.js:111047:37)
at CompilerImpl.compileModuleAsync
(http://localhost:8100/build/vendor.js:76930:49)
at PlatformRef.bootstrapModule
(http://localhost:8100/build/vendor.js:5799:25)
at Object.204 (http://localhost:8100/build/main.js:310:109)
此处 git 代码:ionic app
我要使用 http 提供商。但是当我将 class 注入构造函数时,我得到了错误:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/do';
/*
Generated class for the GetDataProvider provider.
See https://angular.io/guide/dependency-injection for more info on providers
and Angular DI.
*/
@Injectable()
export class GetDataProvider {
private url: string = '192.168.0.110/ajax_inputs';
constructor(private http: HttpClient) {
console.log('Hello GetDataProvider Provider');
}
getData(){
return this.http.get(this.url)
.do(res => console.log(res));
}
}
您需要在 app.module.ts 中导入 HttpClientModule 而不是 HttpClient。 如下图
import { HttpClientModule } from '@angular/common/http';
然后将其添加到导入数组中,如下所示。
imports: [
BrowserModule,
IonicModule.forRoot(MyApp),
//HttpModule,
HttpClientModule
],