axios-登录

1.新建首页index.html

在 resources 目录下 static 中 新建

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>axios异步请求测试</h1>
    <div>        
        <a href="/login.html">登录页面</a>
    </div>

2.新建 login.html

在 resources 目录下 static 中 新建

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <h1>登录页面</h1>
    <input type="text" v-model="user.username" placeholder="用户名">
    <input type="text" v-model="user.password" placeholder="密码">
    <input type="button" value="登录" @click="login()">
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <script>
        let v = new Vue({
            el:"body>div",
            data:{
                user:{
                    username:"",
                    password:""
                }
            },
            methods:{
                login(){
                    axios.post("/login",v.user).then(function (response) {
                        if(response.data==1){
                            alert("登陆成功!");
                            Location.href="/";//显示首页
                        }else if(response.data==2){
                            alert("用户名不存在!");
                        }else{
                            alert("密码错误!");
                        }
                    })
                }

            }
        })
    </script>
</body>
</html>

3.新建User实体类

新建entity包下创建User类 

也可使用lombok注解生成get,set,toString,这里懒得改了

package cn.detu.boot07.entity;
 
public class User {
    private Integer id;
    private String username;
    private String password;
    private String nick;
 
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", nick='" + nick + '\'' +
                '}';
    }
 
    public Integer getId() {
        return id;
    }
 
    public void setId(Integer id) {
        this.id = id;
    }
 
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
 
    public String getNick() {
        return nick;
    }
 
    public void setNick(String nick) {
        this.nick = nick;
    }
}

4.新建UserController

controller 包下新建 控制层
package cn.detu.boot07.controller;

import cn.detu.boot07.entity.User;
import cn.detu.boot07.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired(required = false)
    UserMapper mapper;

    @RequestMapping("/login")
    public int login(@RequestBody User user){
        User u= mapper.selectByUsername(user.getUsername());
        if(u != null){
            if(user.getPassword().equals(u.getPassword())){
                return 1;//登陆成功
            }
            return 3;//密码错误
        }
        return 2;//用户名不存在
    }

5..新建UserMapper

mapper包下新建

package cn.detu.boot07.mapper;
 
import cn.detu.boot07.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
 
@Mapper
public interface UserMapper {
    @Select("select password from user where username = #{username}")
    User selectByUsername(String username);
    
}

6.页面显示

好的,我可以为您提供一个简单的示例代码。请注意,这只是一个基础的示例,您需要根据您的实际需求进行修改。 首先,需要安装以下依赖项: - axios - element-plus 您可以通过以下命令进行安装: ``` npm install axios element-plus ``` 接下来,我们可以在Vue组件中使用axios进行登录请求: ```vue <template> <el-form ref="loginForm" :model="loginForm" label-width="80px"> <el-form-item label="用户名" prop="username"> <el-input v-model="loginForm.username"></el-input> </el-form-item> <el-form-item label="密码" prop="password"> <el-input v-model="loginForm.password" type="password"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="login">登录</el-button> </el-form-item> </el-form> </template> <script> import axios from 'axios'; import { ElMessage } from 'element-plus'; export default { data() { return { loginForm: { username: '', password: '' } }; }, methods: { login() { this.$refs.loginForm.validate(valid => { if (valid) { axios.post('/api/login', this.loginForm) .then(response => { // 登录成功 ElMessage.success('登录成功!'); // TODO: 跳转到首页或其他页面 }) .catch(error => { // 登录失败 ElMessage.error('登录失败!'); }); } else { return false; } }); } } }; </script> ``` 在上面的示例代码中,我们使用了Element Plus的`el-form`和`el-button`组件来创建登录表单。当用户点击“登录”按钮时,我们使用axios发送登录请求。如果登录成功,我们显示成功消息,并跳转到首页或其他页面。如果登录失败,则显示错误消息。 请注意,上面的示例代码中的“/api/login”应该替换为您的后端API的实际URL。 希望这可以帮助您开始编写Vue3应用程序的登录功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值