vue.js登录代码如何写

发布时间:2022-01-26 16:37:31 作者:iii
来源:亿速云 阅读:156

这篇文章主要介绍了vue.js登录代码如何写的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇vue.js登录代码如何写文章都会有所收获,下面我们一起来看看吧。

1.我们通过安装需要的框架和插件,在新的项目中创建.html文件然后再输入代码,设置我们的静态页面,代码如下:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户注册</title>
    <link href="bootstrap-4.3.1-dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="js/jquery.js"></script>
    <script src="bootstrap-4.3.1-dist/js/bootstrap.js"></script>
    <script src="js/vue.js"></script>
    <!-- 异步提交的库 -->
    <script src="js/vue-resource.min.js"></script>
    <style>
        .container {
            margin-top: 15%;
            width: 35%;
        }
        .btn-primary {
            background-color: #337ab7;
            border-color: #337ab7;
        }
        .form-control {
            margin-bottom: 4px;
        }
    </style>
</head>
<body>
<div class="container">
    <!--<div id="demo" v-show="show" class="alert alert-success">
        <span v-if="alert_tips">成功!很好地完成了提交。</span>
    </div>-->
    <form id="form">
        <div class="form-signin">
            <!--<h3 class="form-signin-heading">登录</h3>-->
            <!-- class="sr-only"将label标签隐藏 -->
            <label for="exampleInputUsername" class="sr-only">用户名</label>
            <!-- 会忽略所有表单元素的value、checked、selected特性的初始值,而总是将Vue实例的数据作为数据来源 -->
            <input type="text" class="form-control" v-model="formObj.username" id="exampleInputUsername" name="username"
                   placeholder="用户名">
            <label for="exampleInputUsername" class="sr-only">密码</label>
            <input type="password" class="form-control" v-model="formObj.password" id="exampleInputPassword"
                   name="password"
                   placeholder="密码">        
            <div class="checkbox">
                <label>
                    <!--<input type="checkbox">
                    记住密码-->
                </label>
            </div>
            <button class="btn btn-lg btn-primary btn-block" onclick="ajaxRegister()" type="button">注册
            </button>
        </div>
    </form>
</div>
</body>
<script>
    function ajaxRegister() {
        //Vue的异步Get请求
        /*Vue.http.get("/test").then(function (res) {
            console.log(res.bodyText);
        }, function (res) {
            console.log(res.status);
        });*/
        var param = new FormData(document.getElementById("form"));
       // param = convert_FormData_to_json(param);
        console.log(param);
        Vue.http.post("/login", param).then(function (res) {
            console.log(res.bodyText);
            console.log("登录成功");
        }, function (res) {
            alert("登录失败");
        });
        return false;
    }
</script>
</html>

我们在代码中使用的css内嵌样式与JavaScript代码从而实现我们的效果,

2.既然要实现登录的话,我们就需要使用到数据库,那么下面是有关于数据交互的代码:

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    //通过RequestBody实现与json交互
    @RequestMapping(value = "/register", method = RequestMethod.POST)
     //接收从客户端传过来的FormData()数据
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(HttpServletRequest request) throws ParseException {
        MultipartHttpServletRequest params = (MultipartHttpServletRequest) request;
        // List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        Map<String, String[]> parameterMap = params.getParameterMap();
        //将Map<String,String[]>转成Map对象
        Map map = GenUtils.toParameterMap(parameterMap);
        //将Map对象生成为指定的Pojo对象
        User user = GenUtils.mapGetObj(User.class, map);
        //
        user = userService.selectByUser(user);
        //
        JSONObject jsonObject = JSONObject.fromObject(user);
        return jsonObject + "";
    }
}

关于“vue.js登录代码如何写”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“vue.js登录代码如何写”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. html空格代码如何写
  2. html表格代码如何写

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue.js

上一篇:javascript如何修改元素的style属性

下一篇:@Transactional注解怎么用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》