本节主要是前后端接口的调用,以及前端如何进行封装接口

一、创建相关文件

在文件夹下创建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请求的更多相关文章

  1. 学习版pytest内核测试平台开发万字长文入门篇

    前言 2021年,测试平台如雨后春笋般冒了出来,我就是其中一员,写了一款pytest内核测试平台,在公司落地.分享出来后,有同学觉得挺不错,希望能开源,本着"公司代码不要传到网上去,以免引起 ...

  2. 技术分享 | 一步一步学测试平台开发-Vue restful请求

    本文节选自霍格沃兹测试学院内部教材 一般在构建应用时需要访问后端的 API 接口获取后端数据并展示.做这件事的方法有很多种(比如 axios,vue-resource,fetch-jsonp),使用 ...

  3. 自动化测试平台(Vue前端框架安装配置)

    Vue简介: 通俗的来说Vue是前端框架,用来写html的框架,可轻量级也可不轻量级 Vue特性: 绑定性,响应性,实时性,组件性 安装软件以及控件: 控件库:element-ui node.js ( ...

  4. EIP权限工作流平台总结-2前端框架

      1.预览地址:www.eipflow.com (1) 权限工作流:www.demo.eipflow.com/Account/Login (2) 基础权限版:www.auth.eipflow.com ...

  5. (转)2018几大主流的UI/JS框架——前端框架 [Vue.js(目前市场上的主流)]

    https://blog.csdn.net/hu_belif/article/details/81258961 2016年开始应该是互联网飞速发展的几年,同时也是Web前端开发非常火爆的一年,Web ...

  6. 关于使用jqmobi前端框架在phonegap平台上开发时的日期时间选择控件

    jqmobi(appframework)作为Intel的一款html5移动前端框架,以其自身轻量级和容易上手获得了很多移动HTML5开发者的喜爱,相对于jquerymobile,它可以说将jQuery ...

  7. APICloud APP前端框架——手机APP开发、APP制作、APP定制平台

    概述 APICloud前端框架,包括api.js和api.css.api.css处理不同平台浏览器的默认样式.api.js是一个JavaScript库.是APICloud为混合移动开发定制的轻量Jav ...

  8. 【招聘】.NET高级开发、前端高级开发、测试工程师

    .NET架构师 工作地点:厦门-湖里区 工作年限:5年及以上 学历要求:大专或以上 工资范围:15000元 - 25000元 福利待遇:五险一金,带薪年休假,年度旅游,丰富的员工团队活动:生日会.中秋 ...

  9. Mock平台3-初识Antd React 开箱即用中台前端框架

    微信搜索[大奇测试开],关注这个坚持分享测试开发干货的家伙. 内容提要 首先说下为啥这次测试开发系列教程前端选择Antd React,其实也是纠结对比过最终决定挑战一把,想法大概有几下几点: 笔者自己 ...

  10. Meteor全栈开发平台 - 不仅仅是前端

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博客地址为http://www.cnblogs.com/jasonnode/ .网站上有对应每一 ...

随机推荐

  1. 【论文阅读】ICML2020: Can Autonomous Vehicles Identify, Recover From, and Adapt to Distribution Shifts?

    Column: January 6, 2022 7:18 PM Last edited time: January 30, 2022 12:14 AM Sensor/组织: Oxford Status ...

  2. SpringBoot打包maven仓库里面没有的包

    SpringBoot打包maven仓库里面没有的包 在遇到一些自定义的jar包,maven仓库里面没有这些包,但是发布线上时我们还是只想发布一个jar包,也就是我们想要把第三方包打在最后生成的大jar ...

  3. QT 使用相对路径读取.txt文件

    QT可以使用QFile来读取.txt文件,具体代码实现如下: 1 #include <QCoreApplication> 2 #include <QString> 3 #inc ...

  4. ubuntu20 python2 通过安装包安装mysql-python

    前言 众里寻他千百度,蓦然回首,那人却在灯火阑珊处. 最近要测试一个python2环境的代码,其中用到了MySQLdb, 查询网络,发现要安装mysql-python,于是就进行了一番艰难的探索,现在 ...

  5. Maven的依赖详解和打包方式

    设置maven maven下载与安装教程: https://blog.csdn.net/YOL888666/article/details/122008374 1. 在File->setting ...

  6. 1. 介绍一下 CSS 的盒子模型?

    1. 盒模型: 内容(content).填充(padding).边界(margin). 边框(border) 2. 类型: IE 盒子模型.标准 W3C 盒子模型: 3. 两种盒模型的主要区别是:标准 ...

  7. 2. 标签上 title 与 alt 属性的区别是什么?

    1. alt 是给搜索引擎识别,在图像无法显示时的替代文本2. title 是关于元素的注释信息,主要是给用户解读.3.当鼠标放到文字或是图片上时有 title 文字显示.(因为 IE 不标准)在 I ...

  8. c 语言学习第五天

    break 语句 在循环体中使用 break,可以跳出循环 打印 10 以内的数. #include<stdio.h> int main(){ int i,j = 20; for(i = ...

  9. 说说RabbitMQ延迟队列实现原理?

    使用 RabbitMQ 和 RocketMQ 的人是幸运的,因为这两个 MQ 自身提供了延迟队列的实现,不像用 Kafka 的同学那么苦逼,还要自己实现延迟队列.当然,这都是题外话,今天咱们重点来聊聊 ...

  10. 解码 xsync 的 map 实现

    解码 xsync 的 map 实现 最近在寻找 Go 的并发 map 库的时候,翻到一个 github 宝藏库,xsync (https://github.com/puzpuzpuz/xsync) . ...