无法在 Angular 2 中调用 Promise
Unable to Call Promise in Angular 2
我正在按以下方式在登录 class 中调用服务:
service.ts:
import { Component, OnInit } from '@angular/core';
import { Http, RequestOptions, Headers } from "@angular/http";
import { apimodel } from '../../_model/APImodel';
import Global = require('../../_utilities/constants/constants');
import { UserModel } from '../../_model/usermodel';
import 'rxjs/add/operator/toPromise';
export class services
{
constructor(private http: Http){ }
getLogin(model : UserModel) {
let headers = new Headers({ 'Content-Type': 'application/json; charset=utf-8' });
let options = new RequestOptions({ headers: headers });
return this.http.post(Global.BASE_URL + '/login', model, options)
.toPromise().then(res=><apimodel>res.json()).catch(this.handleError);
}
private handleError(error:any) {
// In a real world app, we might use a remote logging infrastructure
// We'd also dig deeper into the error to get a better message
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.log("Error");
console.error(error); // log to console instead
return Promise.reject(errMsg);
}
}
和login.ts
import { Component, OnInit } from '@angular/core';
import { UserModel } from '../../_model/usermodel';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import Global = require('../../_utilities/constants/constants');
import {apimodel} from "../../_model/APImodel";
import { services } from '../../_utilities/services/services';
@Component({
templateUrl: './_componant/login/login.html'
})
export class LoginComponent implements OnInit{
model : UserModel = {id : 0, email : "", password : "", username : ""};
myform : FormGroup;
submitted : boolean = false;
returnUrl : string;
response : apimodel;
constructor( private route: ActivatedRoute, private router: Router, private services : services){
}
ngOnInit(): void {
this.myform = new FormGroup({
username : new FormControl("", Validators.required),
password : new FormControl("", Validators.required)
})
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
}
login(model : UserModel){
this.submitted = true;
if(this.myform.valid)
{
this.services.getLogin(model).then(result => console.log(result)).catch(error => console.log(error));
//Call Web Service to login
console.log(this.returnUrl);
this.router.navigate([this.returnUrl]);
}
};
}
module.ts
@NgModule({
imports: [BrowserModule, ReactiveFormsModule ,appRouterModule],
declarations: [AppComponent, LoginComponent, RegistrationComponent],
bootstrap: [AppComponent],
providers: [services]
})
错误:
(SystemJS) Can't resolve all parameters for services
如果我从 module.ts 中删除提供商,我会收到以下错误。
No provider for services
更新
我已经像这样添加了@Injectable():
@Injectable()
export class services
{
但我仍然遇到错误:http://prntscr.com/hz3e3a
你不见了@Injectable()
@Injectable()
export class services{
我正在按以下方式在登录 class 中调用服务:
service.ts:
import { Component, OnInit } from '@angular/core';
import { Http, RequestOptions, Headers } from "@angular/http";
import { apimodel } from '../../_model/APImodel';
import Global = require('../../_utilities/constants/constants');
import { UserModel } from '../../_model/usermodel';
import 'rxjs/add/operator/toPromise';
export class services
{
constructor(private http: Http){ }
getLogin(model : UserModel) {
let headers = new Headers({ 'Content-Type': 'application/json; charset=utf-8' });
let options = new RequestOptions({ headers: headers });
return this.http.post(Global.BASE_URL + '/login', model, options)
.toPromise().then(res=><apimodel>res.json()).catch(this.handleError);
}
private handleError(error:any) {
// In a real world app, we might use a remote logging infrastructure
// We'd also dig deeper into the error to get a better message
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.log("Error");
console.error(error); // log to console instead
return Promise.reject(errMsg);
}
}
和login.ts
import { Component, OnInit } from '@angular/core';
import { UserModel } from '../../_model/usermodel';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import Global = require('../../_utilities/constants/constants');
import {apimodel} from "../../_model/APImodel";
import { services } from '../../_utilities/services/services';
@Component({
templateUrl: './_componant/login/login.html'
})
export class LoginComponent implements OnInit{
model : UserModel = {id : 0, email : "", password : "", username : ""};
myform : FormGroup;
submitted : boolean = false;
returnUrl : string;
response : apimodel;
constructor( private route: ActivatedRoute, private router: Router, private services : services){
}
ngOnInit(): void {
this.myform = new FormGroup({
username : new FormControl("", Validators.required),
password : new FormControl("", Validators.required)
})
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
}
login(model : UserModel){
this.submitted = true;
if(this.myform.valid)
{
this.services.getLogin(model).then(result => console.log(result)).catch(error => console.log(error));
//Call Web Service to login
console.log(this.returnUrl);
this.router.navigate([this.returnUrl]);
}
};
}
module.ts
@NgModule({
imports: [BrowserModule, ReactiveFormsModule ,appRouterModule],
declarations: [AppComponent, LoginComponent, RegistrationComponent],
bootstrap: [AppComponent],
providers: [services]
})
错误:
(SystemJS) Can't resolve all parameters for services
如果我从 module.ts 中删除提供商,我会收到以下错误。
No provider for services
更新 我已经像这样添加了@Injectable():
@Injectable()
export class services
{
但我仍然遇到错误:http://prntscr.com/hz3e3a
你不见了@Injectable()
@Injectable()
export class services{