溫馨提示×

溫馨提示×

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

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

vue.js登錄代碼如何寫

發(fā)布時(shí)間:2022-01-26 16:37:31 來源:億速云 閱讀:167 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了vue.js登錄代碼如何寫的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇vue.js登錄代碼如何寫文章都會有所收獲,下面我們一起來看看吧。

1.我們通過安裝需要的框架和插件,在新的項(xiàng)目中創(chuàng)建.html文件然后再輸入代碼,設(shè)置我們的靜態(tài)頁面,代碼如下:

<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標(biāo)簽隱藏 -->
            <label for="exampleInputUsername" class="sr-only">用戶名</label>
            <!-- 會忽略所有表單元素的value、checked、selected特性的初始值,而總是將Vue實(shí)例的數(shù)據(jù)作為數(shù)據(jù)來源 -->
            <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內(nèi)嵌樣式與JavaScript代碼從而實(shí)現(xiàn)我們的效果,

2.既然要實(shí)現(xiàn)登錄的話,我們就需要使用到數(shù)據(jù)庫,那么下面是有關(guān)于數(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對象
        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 + "";
    }
}

關(guān)于“vue.js登錄代碼如何寫”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“vue.js登錄代碼如何寫”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

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

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

AI