我一直在尝试使用 Angular2 发布模拟数据。我今天尝试了以下链接,但没有成功。
https://www.beyondjava.net/blog/mocking-http-services-with-angular-genericly/
下面的链接很好,但我无法使用它
http://embed.plnkr.co/9luTng/?show=preview
在上面的链接中有fake-backend.ts文件,如 app/_helpers/fake-backend.ts
我在 app.module.ts 中包含的假后端但是如何使用它?
所以我想使用如下数据请求数据进行注册:-
{
"username": "string",
"firstname": "string",
"lastname": "string",
"email": "string",
"password": "string",
"authtype": "plain",
"project_license":
"projectlicense"
}
我的回应应该如下: -
{
"message": "A verification mail has been sent to your registered mail."
}
HTML模板如下:-
<div class="modal fade" id="signup-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
<div class="modal-dialog">
<div class="loginmodal-container">
<div class="row">
<h1>Sign Up</h1><br>
<form [formGroup]="signUpForm" (ngSubmit)="onSubmit()">
<div class="col-md-6" id="firstname">
<input type="text" name="firstname" placeholder="First Name" formControlName="firstname">
</div>
<div class="col-md-6" id="lastname">
<input type="text" name="lastname" placeholder="Last Name" formControlName="lastname">
</div>
<input type="text" name="user" placeholder="Enter Username" formControlName="username">
<input type="password" name="pass" placeholder="Password" formControlName="password">
<input type="text" name="email" placeholder="Email Address" formControlName="email">
<input type="text" name="license" placeholder="Project License Key" formControlName="license">
<input logOnClick type="submit" name="login" class="login loginmodal-submit" value="Create An Account">
</form>
<div class="login-help">
<p>
By clicking Create Account you agree to our terms of services & policies.
</p>
</div>
</div>
</div>
</div>
</div>
import { Component, ReflectiveInjector } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { UserService } from '../services/user.service';
import { AlertService } from '../services/alert.service';
import { Http, BaseRequestOptions, Response, ResponseOptions, RequestMethod, XHRBackend, RequestOptions, ConnectionBackend, Headers } from '@angular/http';
import { MockBackend, MockConnection } from '@angular/http/testing';
import { async, fakeAsync, tick } from '@angular/core/testing';
import { fakeBackendFactory } from '../helpers/fake-backend';
@Component({
selector: 'sign-up',
templateUrl: './signup.component.html',
styleUrls: ['./signup.component.css']
})
export class SignUpComponent {
loading = false;
constructor(
private userService: UserService,
private alertService: AlertService) { }
signUpForm = new FormGroup({
firstname: new FormControl('Lexo', Validators.required),
lastname: new FormControl('Luthor', Validators.required),
username: new FormControl('lex', Validators.required),
password: new FormControl('batman123', Validators.required),
email: new FormControl('darkknight@gmail.com', Validators.required),
license: new FormControl('xyz', Validators.required)
})
// injector = ReflectiveInjector.resolveAndCreate([
// {provide: ConnectionBackend, useClass: MockBackend},
// {provide: RequestOptions, useClass: BaseRequestOptions},
// Http,
// UserService,
// ]);
//_userService = this.injector.get(UserService);
//backend:MockBackend = this.injector.get(ConnectionBackend) as MockBackend;
//backend.connections.subscribe((connection: any) => this.lastConnection = connection);
onSubmit(){
console.log("Form value", this.signUpForm.value);
this.loading = true;
this.userService.create(this.signUpForm.value).
subscribe(
data => {
console.log(data);
alert("")
},
error => {
this.alertService.error(error.body);
this.loading = false;
});
let myModal:HTMLElement = document.getElementById("signup-modal");
myModal.classList.toggle("in");
myModal.style.display = "none";
}
}
我的注册服务如下:-
我不想在 localStorage 中存储任何东西,而只是想在注册时得到一个响应,就像上面的响应一样。
import { Injectable, ReflectiveInjector } from '@angular/core';
import { User } from '../models/user';
import { Http, BaseRequestOptions, Response, ResponseOptions, RequestMethod, XHRBackend, RequestOptions, ConnectionBackend, Headers } from '@angular/http';
import { MockBackend, MockConnection } from '@angular/http/testing';
import {async, fakeAsync, tick} from '@angular/core/testing';
@Injectable()
export class UserService {
connection: MockConnection;
users = localStorage.getItem("users");
constructor(private http: Http) { }
create(userInput: User) {
let users = JSON.parse(localStorage.getItem('users'));
console.log("users", users);
console.log("userinput" ,userInput);
userInput.authtype = "authtype";
userInput.project_license = "projectlicense";
let arrayOfUsers = [];
arrayOfUsers.push(userInput);
if(users == null || users == undefined) {
localStorage.setItem('users', JSON.stringify(arrayOfUsers));
}else {
users.push(userInput);
localStorage.setItem('users', JSON.stringify(users));
}
return this.http.post('/api/users', userInput, this.jwt()).map((response: Response) => response.json());
}
// private helper methods
private jwt() {
// create authorization header with jwt token
let currentUser = JSON.parse(localStorage.getItem('currentUser'));
if (currentUser && currentUser.token) {
let headers = new Headers({ 'Authorization': 'Bearer ' + currentUser.token });
return new RequestOptions({ headers: headers });
}
}
}