【发布时间】:2018-11-28 01:35:13
【问题描述】:
我正在尝试使用 angular6 将一些表单详细信息发送到后端 API,但它没有按我预期的那样工作,我该如何添加 Observable,我希望使用 observable 而不是 Promise。
login.component.html
loginUser(e){
var username = e.target.elements[0].value;
var password = e.target.elements[1].value;
console.log(username,password);
this.user.setUserLoggedIn()
var body = "username=" + username + "password=" + password;
this.http.post("http://localhost:8080/user/all", body).subscribe((data) => {});
this.router.navigate(['fullpage'])
return false;
}
login.component.html
<form id="loginForm" novalidate="novalidate" (submit)="loginUser($event)">
<div class="form-group">
<label for="username" class="control-label">Username</label>
<input type="text" class="form-control" id="username" name="username" value="" required="" title="Please enter you username" placeholder="example@gmail.com">
<span class="help-block"></span>
</div>
<div class="form-group">
<label for="password" class="control-label">Password</label>
<input type="password" class="form-control" id="password" name="password" value="" required="" title="Please enter your password" placeholder="******">
<span class="help-block"></span>
</div>
<div id="loginErrorMsg" class="alert alert-error hide">Wrong username or password</div>
<div class="checkbox">
</div>
<button type="submit" class="btn btn-success btn-block">Login</button>
</form>
错误:
ERROR 错误:未捕获(承诺):错误: StaticInjectorError(AppModule)[LogincomponentComponent -> Http]:
StaticInjectorError(Platform: core)[LogincomponentComponent -> Http]: NullInjectorError:没有 Http 提供者!错误:StaticInjectorError(AppModule)[LogincomponentComponent -> Http]:
StaticInjectorError(Platform: core)[LogincomponentComponent -> Http]: NullInjectorError: 没有 Http 提供者!
【问题讨论】:
标签: javascript angular rxjs observable angular6