溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

vuejs如何進(jìn)行設(shè)置登錄

發(fā)布時(shí)間:2021-09-24 17:17:07 來源:億速云 閱讀:107 作者:柒染 欄目:編程語言

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)vuejs如何進(jìn)行設(shè)置登錄,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

vuejs設(shè)置登錄功能的方法:1、編寫html文件;2、通過“vue-resource.js”庫向后臺(tái)提交數(shù)據(jù);3、通過“public class UserController {...}”接收數(shù)據(jù)即可。

本文操作環(huán)境:Windows7系統(tǒng)、vue2.9.6版,DELL G3電腦。

vuejs怎么設(shè)置登錄?

Vue.js實(shí)現(xiàn)登錄功能

編寫html,通過vue-resource.js庫向后臺(tái)提交數(shù)據(jù)

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>用戶注冊(cè)</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>
    <!--<div id="demo" v-show="show" class="alert alert-success">
        <span v-if="alert_tips">成功!很好地完成了提交。</span>
    </div>-->
    <form id="form">
        <div>
            <!--<h3>登錄</h3>-->
            <!--將label標(biāo)簽隱藏 -->
            <label for="exampleInputUsername">用戶名</label>
            <!-- 會(huì)忽略所有表單元素的value、checked、selected特性的初始值,而總是將Vue實(shí)例的數(shù)據(jù)作為數(shù)據(jù)來源 -->
            <input type="text" v-model="formObj.username" id="exampleInputUsername" name="username"
                   placeholder="用戶名">
            <label for="exampleInputUsername">密碼</label>
            <input type="password" v-model="formObj.password" id="exampleInputPassword"
                   name="password"
                   placeholder="密碼">
           
            <div>
                <label>
                    <!--<input type="checkbox">
                    記住密碼-->
                </label>
            </div>
            <button class="btn btn-lg btn-primary btn-block" onclick="ajaxRegister()" type="button">注冊(cè)
            </button>
        </div>
    </form>
</div>
</body>
<script>
    function ajaxRegister() {
        //Vue的異步Get請(qǐng)求
        /*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>

后臺(tái)接收數(shù)據(jù)

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    //通過RequestBody實(shí)現(xiàn)與json交互
    @RequestMapping(value = "/register", method = RequestMethod.POST)
     //接收從客戶端傳過來的FormData()數(shù)據(jù)
    @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[]>轉(zhuǎn)成Map對(duì)象
        Map map = GenUtils.toParameterMap(parameterMap);
        //將Map對(duì)象生成為指定的Pojo對(duì)象
        User user = GenUtils.mapGetObj(User.class, map);
        //
        user = userService.selectByUser(user);
        //
        JSONObject jsonObject = JSONObject.fromObject(user);
        return jsonObject + "";
    }
}


上述就是小編為大家分享的vuejs如何進(jìn)行設(shè)置登錄了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI