【测试平台开发】——07Vue前端框架实战——restful请求
本节主要是前后端接口的调用,以及前端如何进行封装接口
一、创建相关文件
在文件夹下创建http.js、api.js、user.js

1)http.js封装接口:
在src下创建api文件夹
添加http.js文件
创建一个通用的aixos实例
设置baseURL,headers
import axios from 'axios'
var instance = axios.create({
headers: {
'Content-Type': 'application/json'
},
baseURL:'http://xxx.xxxx.com:8089/'
}) export default instance
2)user.js
创建接口,引用http.js文件
import axios from './http' // 定义常量
const user = {
//创建登录接口
signIn(){ }} export default user
3)api.js
把每个界面的接口都统一封装一个js里
import user from "./user"
const api = {
user
}
export default api
二、挂载
1)main.js
把api挂载到这里,通过引用vue,就可以引用api里面的接口
import Vue from 'vue'
import './plugins/axios'
import App from './App.vue'
import router from './router'
import store from './store'
import vuetify from './plugins/vuetify'
import api from './api/api.js' Vue.config.productionTip = false
Vue.prototype.$api = api new Vue({
router,
store,
vuetify,
render: h => h(App)
}).$mount('#app')
三、接口文档--登录、注册
1)登录接口

通过接口文档,完善user.js接口:
import axios from './http' // 定义常量
const user = {
//登录接口
signIn(params){
return axios.post('/user/login',params)
}
//
} export default user
2)注册接口

import axios from './http' // 定义常量
const user = {
//登录接口
signIn(params){
return axios.post('/user/login',params)
},
//注册接口
signUp(params){
return axios.post('/user/register',params)
}
} export default user
四、前端调用后端接口
1)创建SingUp.vue文件

vue文件三部分模板
// html部分
<template>
<div>
注册
</div>
</template> // JS部分
<script>
export default { }
</script> // CSS部分
<style lang="stylus" scoped> </style>
2)设置路由
import Vue from 'vue'
import VueRouter from 'vue-router'
import SignIn from '../components/SignIn.vue'
import SignUp from '../components/SignUp.vue' Vue.use(VueRouter) const routes = [
{
path: '/',
name: 'SignIn',
component: SignIn
},
{
path: '/',
name: 'SignUp',
component: SignUp
}
] const router = new VueRouter({
routes
}) export default router
3)登录界面跳转注册页面
登录界面的【注册】按钮定义方法,并且在js方法中添加跳转方法,红色标注
// Html部分
<template>
<div class="login-form">
<h1>登录</h1>
<v-col cols="12">
<v-text-field
v-model="username"
name = 'input-10-0'
label="用户名"
placeholder="请输入用户名"
outlined
:rules="[rules.required, rules.nameMin]"
hint="至少8个字符"
counter
></v-text-field>
</v-col> <v-col cols="12">
<v-text-field
v-model="password"
:append-icon="show1 ? 'mdi-eye' : 'mdi-eye-off'"
:rules="[rules.required, rules.pwdMin]"
:type="show1 ? 'text' : 'password'"
name="input-10-1"
label="密码"
placeholder="请输入密码"
hint="至少6个字符"
outlined
counter
@click:append="show1 = !show1"
></v-text-field>
</v-col> <v-btn depressed color="primary" elevation="12" large>立即登录</v-btn>
<v-btn class="btn2" depressed color="primary" elevation="12" large @click="singUp()">立即注册</v-btn>
</div>
</template> // JS部分
<script>
export default {
data () {
return {
show1: false,
show2: false,
show3: false,
show4: false,
username: '',
password: '',
rules: {
required: value => !!value || '不能为空',
nameMin: v => v.length >= 8 || '至少8个字符',
pwdMin: v => v.length >= 6 || '至少6个字符',
emailMatch: () => (`The email and password you entered don't match`),
},
}
},
methods:{
singUp(){
this.$router.push({name: 'SignUp'})
}
}
}
</script> // CSS部分
<style scoped>
.login-form{
width: 500px;
margin: 0 auto;
text-align: center;
margin-top: 100px;
}
.btn2{
margin-left: 30px;
}
</style>
跳转效果:

4)完善注册页面
接口文档里面注册接口需要:邮箱、用户名、密码
页面构建跟登录页面差不多,就直接上代码了,包括返回按钮跳转登录页面
// html
<template>
<div class="logon-form">
<h1>注册</h1> <v-col cols="12">
<v-text-field
v-model="username"
name = 'input-10-0'
label="用户名"
placeholder="请输入用户名"
outlined
:rules="[rules.required, rules.nameMin, rules.inputMax]"
hint="至少8个字符,最多20个字符"
counter
></v-text-field>
</v-col> <v-col cols="12">
<v-text-field
v-model="password"
:append-icon="show1 ? 'mdi-eye' : 'mdi-eye-off'"
:rules="[rules.required, rules.pwdMin, rules.inputMax]"
:type="show1 ? 'text' : 'password'"
name="input-10-1"
label="密码"
placeholder="请输入密码"
hint="至少6个字符,最多20个字符"
outlined
counter
@click:append="show1 = !show1"
></v-text-field>
</v-col> <v-col cols="12">
<v-text-field
v-model="email"
name = 'input-10-2'
label="邮箱"
placeholder="请输入邮箱"
outlined
:rules="[rules.required, rules.inputMax]"
hint="最多20个字符"
counter
></v-text-field>
</v-col> <v-btn class="style-btn" depressed color="primary" elevation="12" large>注册</v-btn>
<v-btn class="back-btn style-btn" depressed color="primary" elevation="12" large @click="back()">返回</v-btn>
</div>
</template> // JS
<script>
export default {
data () {
return {
show1: false,
show2: false,
show3: false,
show4: false,
username: '',
password: '',
email: '',
rules: {
required: value => !!value || '不能为空',
nameMin: v => v.length >= 8 || '至少8个字符',
pwdMin: v => v.length >= 6 || '至少6个字符',
inputMax: v => v.length <= 20 || '最多20个字符',
emailMatch: () => (`The email and password you entered don't match`),
},
}
},
methods:{
back(){
this.$router.push({name: 'SignIn'})
}
}
}
</script> // CSS
<style lang="stylus" scoped>
.logon-form{
width: 500px;
margin: 0 auto;
text-align: center;
margin-top: 100px;
}
.back-btn{
margin-left: 30px;
}
.style-btn{
width: 100px;
}
</style>
展示效果:

5)前端调用注册接口
// html
<template>
<div class="logon-form">
<h1>注册</h1>
。。。。。。省略 <v-btn class="style-btn" depressed color="primary" elevation="12" large @click="register()">注册</v-btn>
<v-btn class="back-btn style-btn" depressed color="primary" elevation="12" large @click="back()">返回登录</v-btn>
</div>
</template> // JS
<script>
export default {
data () {
return {
。。。。。。省略
},
}
},
methods:{
back(){
this.$router.push({name: 'SignIn'})
},
register(){
let post_data = {
username : this.username,
password : this.password,
email : this.email
}
this.$api.user.signUp(post_data).then(res=>{
console.log(res)
})
}
},
}
</script>
</style>
页面填写值,点击【注册】按钮

解析:
register方法有两部分组成。
1、传递参数
let post_data = {
username : this.username,
password : this.password,
email : this.email
}
2、调取接口
this.$api.user.signUp(post_data).then(res=>{
console.log(res)
})
6)调用登录接口
点击【登录】按钮,调取登录接口
// Html部分
<template>
<div class="login-form">
<h1>登录</h1>
<v-col cols="12">
<v-text-field
v-model="username"
name = 'input-10-0'
label="用户名"
placeholder="请输入用户名"
outlined
:rules="[rules.required, rules.nameMin, rules.inputMax]"
hint="至少8个字符,最多20字符"
counter
></v-text-field>
</v-col> <v-col cols="12">
<v-text-field
v-model="password"
:append-icon="show1 ? 'mdi-eye' : 'mdi-eye-off'"
:rules="[rules.required, rules.pwdMin, rules.inputMax]"
:type="show1 ? 'text' : 'password'"
name="input-10-1"
label="密码"
placeholder="请输入密码"
hint="至少6个字符,最多20字符"
outlined
counter
@click:append="show1 = !show1"
></v-text-field>
</v-col> <v-btn class="style-btn" depressed color="primary" elevation="12" large @click="login()">立即登录</v-btn>
<v-btn class="logon-btn style-btn" depressed color="primary" elevation="12" large @click="singUp()">立即注册</v-btn>
</div>
</template> // JS部分
<script>
export default {
data () {
return {
show1: false,
show2: false,
show3: false,
show4: false,
username: '',
password: '',
rules: {
required: value => !!value || '不能为空',
nameMin: v => v.length >= 8 || '至少8个字符',
pwdMin: v => v.length >= 6 || '至少6个字符',
inputMax: v => v.length <= 20 || '最多20个字符',
emailMatch: () => (`The email and password you entered don't match`),
},
}
},
methods:{
singUp(){
this.$router.push({name: 'SignUp'})
},
login(){
let post_data = {
username : this.username,
password : this.password
}
this.$api.user.signIn(post_data).then(res=>{
console.log(res)
})
}
},
}
</script> // CSS部分
<style scoped>
.login-form{
width: 500px;
margin: 0 auto;
text-align: center;
margin-top: 100px;
}
.logon-btn{
margin-left: 30px;
}
.style-btn{
width: 100px;
}
</style>
内容与注册同理

【测试平台开发】——07Vue前端框架实战——restful请求的更多相关文章
- 学习版pytest内核测试平台开发万字长文入门篇
前言 2021年,测试平台如雨后春笋般冒了出来,我就是其中一员,写了一款pytest内核测试平台,在公司落地.分享出来后,有同学觉得挺不错,希望能开源,本着"公司代码不要传到网上去,以免引起 ...
- 技术分享 | 一步一步学测试平台开发-Vue restful请求
本文节选自霍格沃兹测试学院内部教材 一般在构建应用时需要访问后端的 API 接口获取后端数据并展示.做这件事的方法有很多种(比如 axios,vue-resource,fetch-jsonp),使用 ...
- 自动化测试平台(Vue前端框架安装配置)
Vue简介: 通俗的来说Vue是前端框架,用来写html的框架,可轻量级也可不轻量级 Vue特性: 绑定性,响应性,实时性,组件性 安装软件以及控件: 控件库:element-ui node.js ( ...
- EIP权限工作流平台总结-2前端框架
1.预览地址:www.eipflow.com (1) 权限工作流:www.demo.eipflow.com/Account/Login (2) 基础权限版:www.auth.eipflow.com ...
- (转)2018几大主流的UI/JS框架——前端框架 [Vue.js(目前市场上的主流)]
https://blog.csdn.net/hu_belif/article/details/81258961 2016年开始应该是互联网飞速发展的几年,同时也是Web前端开发非常火爆的一年,Web ...
- 关于使用jqmobi前端框架在phonegap平台上开发时的日期时间选择控件
jqmobi(appframework)作为Intel的一款html5移动前端框架,以其自身轻量级和容易上手获得了很多移动HTML5开发者的喜爱,相对于jquerymobile,它可以说将jQuery ...
- APICloud APP前端框架——手机APP开发、APP制作、APP定制平台
概述 APICloud前端框架,包括api.js和api.css.api.css处理不同平台浏览器的默认样式.api.js是一个JavaScript库.是APICloud为混合移动开发定制的轻量Jav ...
- 【招聘】.NET高级开发、前端高级开发、测试工程师
.NET架构师 工作地点:厦门-湖里区 工作年限:5年及以上 学历要求:大专或以上 工资范围:15000元 - 25000元 福利待遇:五险一金,带薪年休假,年度旅游,丰富的员工团队活动:生日会.中秋 ...
- Mock平台3-初识Antd React 开箱即用中台前端框架
微信搜索[大奇测试开],关注这个坚持分享测试开发干货的家伙. 内容提要 首先说下为啥这次测试开发系列教程前端选择Antd React,其实也是纠结对比过最终决定挑战一把,想法大概有几下几点: 笔者自己 ...
- Meteor全栈开发平台 - 不仅仅是前端
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博客地址为http://www.cnblogs.com/jasonnode/ .网站上有对应每一 ...
随机推荐
- Python读取YAML配置数据
python编写的一些脚本需要一些简单配置时可以使用yaml文件进行设置.本文将介绍如何使用pyyaml进行读取配置数据. 首先安装pyyaml pip install pyyaml 简单使用下pyy ...
- Vue 组件间通信有哪几种方式?
父子通信 (1)父组件向子组件传值props <button-counter :title="send"></button-counter> Vue.com ...
- JavaScript -- 数据类型 --手稿
- Exception in thread "main" java.lang.NoClassDefFoundError: io/netty/channel/EventLoopGroup
最近在学习dubbo,跟着教程做,但是运行时报错,需要添加netty依赖 <dependency> <groupId>io.netty</groupId> < ...
- SpringBoot全局异常,返回JSON数据
全局异常处理 为什么要配全局异常? 不配全局服务端报错场景,1/0.空指针等 配置好处 统一的错误页面或错误码 对用户更友好 配置全局异常 第一步类添加注解 @ControllerAdvicce,如果 ...
- 关于ComfyUI的一些Tips
关于ComfyUI的一些Tips 前言: 最近发的ComfyUI相关文章节奏不知道会不会很快,在创作的时候没有考虑很多,想着把自己的知识分享出去.后台也看到很多私信,有各种各样的问题,这是我欠缺考虑了 ...
- 推荐一款Python接口自动化测试数据提取分析神器!
1.引言 在处理JSON数据时,我们常常需要提取.筛选或者变换数据.手动编写这些操作的代码不仅繁琐,而且容易出错.Python作为一个功能强大的编程语言,拥有丰富的库和工具来处理这些数据.今天,将介绍 ...
- [oeasy]python0021_python虚拟机的位置_可执行文件_转化为字节形态
程序本质 回忆上次内容 \n 就是换行 他对应着 ascii 字符的代码是(10)10进制 他的英文是 LF,意思是Line Feed 我们可以在<安徒 ...
- laravel6学习
web 服务器需要拥有 storage 目录下的所有目录和 bootstrap/cache 目录的写权限
- Spring 常用的三种拦截器详解
前言 在开发过程中,我们常常使用到拦截器来处理一些逻辑.最常用的三种拦截器分别是 AOP. Interceptor . Filter,但其实很多人并不知道什么时候用AOP,什么时候用Intercept ...