iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Vue项目如何创建首页发送axios请求
  • 201
分享到

Vue项目如何创建首页发送axios请求

2023-07-05 04:07:42 201人浏览 安东尼
摘要

这篇文章主要介绍了Vue项目如何创建首页发送axiOS请求的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Vue项目如何创建首页发送axios请求文章都会有所收获,下面我们一起来看看吧。这是个全新的Vue项目,引

这篇文章主要介绍了Vue项目如何创建首页发送axiOS请求的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Vue项目如何创建首页发送axios请求文章都会有所收获,下面我们一起来看看吧。

这是个全新的Vue项目,引入了ElementUI

Vue项目如何创建首页发送axios请求

 将App.vue里的内容干掉,剩如下

Vue项目如何创建首页发送axios请求

然后下面的三个文件也可以删掉了

Vue项目如何创建首页发送axios请求

Vue项目如何创建首页发送axios请求

在views文件下新建Login.vue组件

Vue项目如何创建首页发送axios请求

 到router目录下的index.js

Vue项目如何创建首页发送axios请求

 那么现在的流程大概是这样子的

Vue项目如何创建首页发送axios请求

 启动

Vue项目如何创建首页发送axios请求

 写登陆页面

<template>  <div>    <el-fORM :ref="form" :model="loginForm" class="loginContainer">      <h4 class="loginTitle">系统登录</h4>      <!-- auto-complete="false"自动补全 -->      <el-form-item label="">           <el-input type="text" auto-complete="false" v-model="loginForm.username" placeholder="请输入用户名"></el-input>      </el-form-item>      <el-form-item label="">        <el-input type="text" auto-complete="false" v-model="loginForm.passWord" placeholder="请输入密码"></el-input>      </el-form-item>      <el-form-item label="">        <el-input type="text" auto-complete="false" v-model="loginForm.code" placeholder="点击图片更换验证码" ></el-input>        <img :src="captchaUrl"/>       </el-form-item>       <el-checkbox v-model="checked" class="loginRemeber">记住我</el-checkbox>       <el-button type="primary" >登录</el-button>    </el-form>  </div></template> <script>export default {    name:"Login",    data(){        return{            captchaUrl:'',//验证码图片链接            loginForm:{                username:'admin',                password:'123456',                code:'1234'            },            checked:true        }    } }</script> <style>    .loginContainer{        border-radius: 15px;        background-clip: padding-box;        margin:180px auto;        width:350px;        padding: 15px 35px 15px 35px;        background: #a8dad5;        border:1px solid #eaeaea;        box-shadow: 0 0 25px #cac6c6;    }    .loginTitle{        margin: 0px auto 40px auto;        text-align: center;    }    .loginRemeber{        text-align: left;        margin:0px 0px 15px 0px;    }</style>

Vue项目如何创建首页发送axios请求

给登录按钮添加点击事件

Vue项目如何创建首页发送axios请求

添加方法

Vue项目如何创建首页发送axios请求

Vue项目如何创建首页发送axios请求

 添加表单校验  暂时先吧:ref="form"去掉

Vue项目如何创建首页发送axios请求

Vue项目如何创建首页发送axios请求

校验的username,password,code需要和上面的对应上 需要加prop属性

Vue项目如何创建首页发送axios请求

测试,校验规则是存在的,但是出现的问题是点击表单还是生效的

Vue项目如何创建首页发送axios请求

在点击登录时候添加表单校验

Vue项目如何创建首页发送axios请求

Vue项目如何创建首页发送axios请求

会自动根据我们自己定义的校验规则来校验,还是将用户名长度改成5位好了 

Vue项目如何创建首页发送axios请求

Vue项目如何创建首页发送axios请求

Vue项目如何创建首页发送axios请求

用ElementUI的showMessage

Vue项目如何创建首页发送axios请求

Vue项目如何创建首页发送axios请求

效果如下

Vue项目如何创建首页发送axios请求

接下来需要发送axios请求

安装axios

Vue项目如何创建首页发送axios请求

安装完成,可以在package.JSON文件看到

Vue项目如何创建首页发送axios请求

 组件里引入

Vue项目如何创建首页发送axios请求

Vue项目如何创建首页发送axios请求

 这里我随便建个后端,先进行演示,会出现跨域现象,这里跨域先不讲

Vue项目如何创建首页发送axios请求

 看下返回的信息里有什么

Vue项目如何创建首页发送axios请求

<template>  <div>    <el-form :rules="rules" ref="form" :model="loginForm" class="loginContainer">      <h4 class="loginTitle">系统登录</h4>      <!-- auto-complete="false"自动补全 -->      <el-form-item prop="username">           <el-input type="text" auto-complete="false" v-model="loginForm.username" placeholder="请输入用户名"></el-input>      </el-form-item>      <el-form-item prop="password">        <el-input type="text" auto-complete="false" v-model="loginForm.password" placeholder="请输入密码"></el-input>      </el-form-item>      <el-form-item prop="code">        <el-input type="text" auto-complete="false" v-model="loginForm.code" placeholder="点击图片更换验证码" ></el-input>        <img :src="captchaUrl"/>       </el-form-item>       <el-checkbox v-model="checked" class="loginRemeber">记住我</el-checkbox>       <el-button type="primary"  @click="submitLogin">登录</el-button>    </el-form>  </div></template>  <script>import axios from 'axios'export default {    name:"Login",    data(){        return{            captchaUrl:'',//验证码图片链接            loginForm:{                username:'admin',                password:'123456',                code:'1234'            },            checked:true,            rules:{                username:[                    {required:true,message:'请输入用户名',trigger:'blur'},                    {min:5,max:12,message:'用户名长度6到12位',trigger:'blur'}                ],                password:[                    {required:true,message:'请输入密码',trigger:'blur'},                    {min:6,max:12,message:'密码长度6到12位',trigger:'blur'}                ],                code:[                    {required:true,message:'请输入验证码',trigger:'blur'},                    {min:4,max:4,message:'验证码长度4位',trigger:'blur'}                ],            }        }    },    methods:{        submitLogin(){            this.$refs.form.validate((valid)=>{                if(valid){                    axios.post('Http://localhost:8081/demo',{username:"xxx",password:"123456",code:"1234"}).then((res)=>{                        console.log(res)                                            })                }else{                    this.$message.error('请输入正确格式')                    return false                }                })        }    } }</script> <style>    .loginContainer{        border-radius: 15px;        background-clip: padding-box;        margin:180px auto;        width:350px;        padding: 15px 35px 15px 35px;        background: #a8dad5;        border:1px solid #eaeaea;        box-shadow: 0 0 25px #cac6c6;    }    .loginTitle{        margin: 0px auto 40px auto;        text-align: center;    }    .loginRemeber{        text-align: left;        margin:0px 0px 15px 0px;    }</style>

关于“Vue项目如何创建首页发送axios请求”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Vue项目如何创建首页发送axios请求”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程网精选频道。

--结束END--

本文标题: Vue项目如何创建首页发送axios请求

本文链接: https://www.lsjlt.com/news/349524.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • MySQL怎么更新字段长度
    要更新MySQL表中的字段长度,可以使用ALTER TABLE语句。以下是更新字段长度的示例: ALTER TABLE table_...
    99+
    2024-05-14
    mysql
  • sql如何修改表字段长度
    要修改表字段的长度,可以使用ALTER TABLE语句,然后使用MODIFY COLUMN来修改字段的长度。以下是一个示例: ALT...
    99+
    2024-05-14
    sql
  • mysql数据量太大如何解决
    有几种方法可以解决MySQL数据量过大的问题: 数据库分区:将数据库表按照某种规则进行分区,分成多个小表,可以提高查询速度和管理...
    99+
    2024-05-14
    mysql
  • MySQL单表数据量过大如何解决
    在MySQL中,当单表的数据量过大时,可以采取以下几种方法来解决问题: 使用索引:在表的字段上创建适当的索引,可以提高查询效率,...
    99+
    2024-05-14
    mysql
  • mysql导出数据量过大如何解决
    当导出的数据量过大时,可以尝试以下方法来解决问题: 使用数据库工具:使用数据库管理工具如Navicat或MySQL Workbe...
    99+
    2024-05-14
    mysql
  • mysql中log_bin的作用是什么
    在MySQL中,log_bin是二进制日志文件的名称,用于记录所有对数据库进行更改的操作,如插入、更新、删除等。这些二进制日志文件可...
    99+
    2024-05-14
    mysql
  • mysql中log_bin的特点有哪些
    log_bin是MySQL的二进制日志文件,用于记录所有对数据库的修改操作,包括插入、更新、删除等。 log_bin文件是M...
    99+
    2024-05-14
    mysql
  • mysql rpm包安装的方法是什么
    要在Linux系统上安装MySQL RPM包,可以按照以下步骤操作: 下载MySQL的RPM包:你可以从MySQL官方网站下载适...
    99+
    2024-05-14
    mysql
  • mysql rpm包安装后怎么使用
    安装MySQL RPM包后,您可以通过以下步骤来使用MySQL: 启动MySQL服务:使用以下命令来启动MySQL服务: sud...
    99+
    2024-05-14
    mysql
  • lxml中怎么处理XML命名空间默认值
    在lxml中处理XML命名空间的默认值可以通过使用xpath()方法和register_namespace()方法来实现。...
    99+
    2024-05-14
    lxml
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作