Angular4+NodeJs+MySQL 入门-05 接口调用
接口调用今天讲一下,如果在前端页面上通过调用后台接口,返回来的数据。把前面的几章结合起来。 简单介绍一下 https://github.com/xiaotuni/angular-map-http2 这个项目吧
好了现在开始正式来调用了。以用户登录来讲吧, 首先是画一个登录界面html ->login.html内容<xtn-navbar [(Title)]="__Title"></xtn-navbar>
<div class="loginCss">
<div class="top"></div>
<div class="ctrl">
<div class="username">
<input type="text" placeholder="Enter username" required [(ngModel)]="UserInfo.username">
</div>
<div class="password">
<input type="password" placeholder="Enter password" required [(ngModel)]="UserInfo.password">
</div>
</div>
<div class="operator">
<div class="btn" (click)="submit()">
<div class="login">
login
</div>
</div>
<div class="btn">
<div class="forget" (click)="forgetPassword()">
forget
</div>
</div>
</div>
</div>
Css –>login.scss 内容.loginCss {
padding: 10px;
.ctrl {
margin-top: 20vh;
position: relative;
.username {
position: relative;
display: flex;
input {
border: 1px solid #f5f5f5;
padding: 5px 10px;
border-radius: 5px;
flex: 1;
font-size: 1rem;
}
}
.password {
@extend .username;
margin-top: 2rem;
}
}
.operator {
display: flex;
padding: 2rem;
.btn {
margin: 5px;
flex: 1;
text-align: center;
padding: 5px 10px;
> div {
padding: 5px 10px;
border: 1px solid #999;
&:hover {
border: 1px solid blue;
}
}
}
}
}
最后就是typescript了–>login.ts内容import { Component,OnInit,Output,Input } from '@angular/core';
import { Utility,ServiceHelper,routeAnimation,BaseComponent } from '../Core';
import * as CryptoJS from 'crypto-js';
@Component({
selector: 'xtn-manage-login',templateUrl: './login.html',styleUrls: ['./login.scss'],animations: [routeAnimation],// 页面切换动画
providers: [ServiceHelper] // 注入一个service
})
export class Login extends BaseComponent implements OnInit {
public UserInfo: any;
/** * Creates an instance of Login. * @param {ServiceHelper} sHelper service用于接口调用等 * @memberof Login */
constructor(private sHelper: ServiceHelper) {
super();
this.UserInfo = { username: 'admin',password: 'admin@163.com' };
}
ngOnInit() {
}
/** * 点击登录按钮 * * @memberof Login */
submit() {
const data = Object.assign({},this.UserInfo);
data.password = CryptoJS.MD5(data.password).toString();
this.sHelper.UserInfo.Login(data).then(() => {
const { Params } = Utility.$GetContent(Utility.$ConstItem.UrlPathInfo) || { Params: {} };
const { IsGoBack } = Params || { IsGoBack: 0 };
if (!!Number(IsGoBack)) {
Utility.$GoBack();
} else {
Utility.$ToPage(Utility.$ConstItem.UrlItem.ManagerDashboard,{});
}
},() => { });
}
forgetPassword() {
console.log('forgetPassword');
}
}
ServiceHelper 里的代码这个里面的代码其实很简单的,就是一个入口,有的时候一个组件要引用好多service,在构造函数里要好多 constructor(private service1: Service1,…) {}。我就把这些都放到一个里去。里面的代码如: import { Injectable } from '@angular/core';
import { Client } from './Core';
import { ApiManagerService } from './ApiManager';
import { UserInfoService } from './UserInfo';
@Injectable()
export class ServiceHelper {
public ApiManager: ApiManagerService;
public UserInfo: UserInfoService;
constructor() {
this.ApiManager = new ApiManagerService(Client);
this.UserInfo = new UserInfoService(Client);
}
}
由于是用户登录,所以用到了UserInfoService这个类。 UserInfoService.ts,用户登录,注册,用户详情等接口调用及数据处理类import { Utility } from './Core';
export class UserInfoService {
public UserInfo: any;
public Users: any[];
constructor(private Client) {
}
/** * 用户登录 * * @param {*} obj * @returns {Promise<any>} * @memberof UserInfoService */
Login(obj: any): Promise<any> {
const __List = { actions: { list: [],loading: 'Load',fail: 'Fail',complete: 'Complete' } };
__List.actions.list.push({
StateName: 'StateName',Condition: obj,promise: (client) => client.post(client.API.UserInfo.Login,{ data: obj }),});
const __self = this;
return this.Client(__List).then((result) => {
__self.UserInfo = result && result[0] ? result[0] : [];
// 将token保存下来。
Utility.$SetContent(Utility.$ConstItem.UserInfo,__self.UserInfo,true);
return result;
});
}
}
接口调用的地址在哪里呢?是: (client) => client.post(client.API.UserInfo.Login,{ data: obj }),而这个又在哪里西的呢,在ApiClient.ts文件里。之前的篇幅说到了,怎么配置。点击登录时,错误密码时出错。 后台接口配置下一篇再说了,只需要添加一条规则文件记录就可以了。(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |