Javascript 角度:没有Http的提供程序
NPM版本:8.1.4 完整错误:Javascript 角度:没有Http的提供程序,javascript,angular,http,typescript,Javascript,Angular,Http,Typescript,NPM版本:8.1.4 完整错误:Error:Uncaught(承诺中):Error:Error在./SignupComponent类SignupComponent_主机中-内联模板:0:0由以下原因引起:没有Http提供程序! 错误:没有Http的提供程序 我知道这个错误通常表明HttpModule在应用程序的某个地方,但是,在我的例子中,它是在app.module中全局提供的 当我将使用构造函数中的Http类的服务导入到我的组件时,我收到了这个错误 我有我的app.module类,该类全局导
Error:Uncaught(承诺中):Error:Error在./SignupComponent类SignupComponent_主机中-内联模板:0:0由以下原因引起:没有Http提供程序!
错误:没有Http的提供程序代码>
我知道这个错误通常表明HttpModule在应用程序的某个地方,但是,在我的例子中,它是在app.module中全局提供的
当我将使用构造函数中的Http类的服务导入到我的组件时,我收到了这个错误
我有我的app.module类,该类全局导入并提供HttpModule
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule} from '@angular/forms';
import { RouterModule } from '@angular/router';
import { HttpModule } from '@angular/Http';
import { AppComponent } from "./components/app/app.component";
import { MessageComponent } from "./components/messages/message.component";
import { MessageListComponent } from "./components/messages/message-list.component";
import { MessageInputComponent } from "./components/messages/message-input.component";
import {MessageService} from "./components/messages/message.service";
import {MessagesComponent} from "./components/messages/messages.component";
import {AuthenticationComponent} from "./components/auth/authentication.component";
import {routing} from "./components/app/routes";
import {HeaderComponent} from "./components/app/header.component";
import {LogoutComponent} from "./components/auth/logout.component";
import {SigninComponent} from "./components/auth/signin.component";
import {SignupComponent} from "./components/auth/signup.component";
import {AuthenticationService} from "./components/auth/authentication.service";
@NgModule({
declarations: [
AppComponent,
MessageComponent,
MessagesComponent,
MessageListComponent,
MessageInputComponent,
AuthenticationComponent,
HeaderComponent,
LogoutComponent,
SigninComponent,
SignupComponent
],
imports: [BrowserModule,
FormsModule,
RouterModule,
HttpModule,
ReactiveFormsModule,
routing],
providers: [MessageService,
AuthenticationService],
bootstrap: [AppComponent]
})
export class AppModule {
}
在我的authentication.service中,我从HttpModule导入Http并在服务中使用它
import {Http, Headers, Response} from "@angular/http";
import {User} from "../../models/user.model";
import 'rxjs/Rx';
import { Observable } from "rxjs/Observable";
import { Injectable } from "@angular/core";
@Injectable()
export class AuthenticationService{
constructor(private http: Http){}
signUp(user: User){
const body = JSON.stringify(user);
const headers = new Headers({'Content-Type' : 'application/json'});
return this.http.post('http://localhost:3000/user', body, {headers: headers})
.map((response: Response) => response.json())
.catch((error: Response) => Observable.throw(error.json()));
}
}
最后,我使用注册。组件用户表单使用身份验证。服务注册用户
import { Component, OnInit } from '@angular/core';
import { FormGroup, Validators, FormControl } from '@angular/forms';
import { AuthenticationService } from "./authentication.service";
import { User } from "../../models/user.model";
@Component({
selector: 'app-signup',
templateUrl: 'signup.component.html',
})
export class SignupComponent implements OnInit {
myForm: FormGroup;
constructor(private authenticationService: AuthenticationService) {
}
onSubmit() {
const user = new User(
this.myForm.value.email,
this.myForm.value.password,
this.myForm.value.firstName,
this.myForm.value.lastName
);
this.authenticationService.signUp(user).subscribe(
data => console.log(data),
error => console.log(error)
);
this.myForm.reset();
}
ngOnInit(){
this.myForm = new FormGroup({
firstName: new FormControl(null, Validators.required),
lastName: new FormControl(null, Validators.required),
email: new FormControl(null, [
Validators.required,
Validators.pattern("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")
]),
password: new FormControl(null, Validators.required)
})
}
}
如果我不将authentication.service导入此组件,则一切正常。显然,这并不是因为我没有像在我的app.module中那样提供HttpModule,所以这是我无法理解的
作为旁注,我在运行npm run build
时确实收到了这个警告,它说明了一些关于Http的内容,但我不知道它到底是什么意思
WARNING in ./~/@angular/Http/src/body.js
There are multiple modules with names that only differ in casing.
This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.
Use equal casing. Compare these module identifiers:
* D:\Cloud Storage\Sync\Development\Projects\Udemy_Angular_Node\node_modules\@angular\Http\src\body.js
Used by 2 module(s), i. e.
D:\Cloud Storage\Sync\Development\Projects\Udemy_Angular_Node\node_modules\@angular\Http\src\static_request.js
* D:\Cloud Storage\Sync\Development\Projects\Udemy_Angular_Node\node_modules\@angular\http\src\body.js
Used by 2 module(s), i. e.
D:\Cloud Storage\Sync\Development\Projects\Udemy_Angular_Node\node_modules\@angular\http\src\static_request.js
谢谢我想你在用Systemjs。我看到您已经从不同的模块导入了HttpModule和Http
import { HttpModule } from '@angular/Http';
import {Http, Headers, Response} from "@angular/http";
在这两种情况下都使用小写。应该如此
import { HttpModule } from '@angular/http';
另见
我猜您使用的是Systemjs。我看到您已经从不同的模块导入了HttpModule和Http
import { HttpModule } from '@angular/Http';
import {Http, Headers, Response} from "@angular/http";
在这两种情况下都使用小写。应该如此
import { HttpModule } from '@angular/http';
另见
不应从“@angular/Http”导入{HttpModule};是否从“@angular/http”导入{HttpModule}?不应该从'@angular/Http'导入{HttpModule};是否从“@angular/http”导入{HttpModule}?