2019-08-15 使用thinkphp5.1、vue.js、axios进行表单验证提交

1.0、vue 阶段代码代码

vue.js的下载链接
https://cn.vuejs.org/v2/guide/installation.html

<form id="login">
            <input name="username" type="text"      placeholder="用户名"    v-model="form.username" >
            <input name="password" type="password"  placeholder="密 码"    v-model="form.password" >
            <input name="key"      type="text"      placeholder="验证码"     v-model="form.key">
            <input type="button"   @click="submitForm()"    value="登 录">
        </form>
        <!---加载vue.js 开发版本--->
        <script src="__PLUGS__vue.js"></script>
        <script>
            window.onload = function () {
                const app = new Vue({
                    el: '#login',   //对应使用id="app"获取信息。
                    data: {
                        form:{
                            username: '',
                            password: '',
                            key: ''
                        },
                    },
                    methods: {
                        submitForm: function () {
                            var $this = this;
                            var FormData =this.form;    //获取表单数据
                            console.log(FormData.key);  //打印验证码字段
                        }   
                    }
                })
            };
        </script>

2.0、添加axios.js 后的代码

axios.js 的链接
http://www.axios-js.com/docs/
axios.js 是一个类似于ajax 的异步请求JavaScript 库。相对来说axios比ajax 更适合现在的api为主要数据交互的环境。
axios.js+vue.js 后代码

        <form id="login">
            <input name="username" type="text"      placeholder="用户名"    v-model="form.username" >
            <input name="password" type="password"  placeholder="密 码"    v-model="form.password" >
            <input name="key"      type="text"      placeholder="验证码"     v-model="form.key">
            <input type="button"   @click="submitForm()"    value="登 录">
        </form>
        <!---加载vue.js 开发版本--->
        <script src="__PLUGS__vue.js"></script>
        <!---加载axios.js 库--->
        <script src="__PLUGS__axios.min.js"></script>
        <script>
            window.onload = function () {
                const app = new Vue({
                    el: '#login',   //对应使用id="app"获取信息。
                    data: {
                        form:{
                            username: '',
                            password: '',
                            key: ''
                        },
                    },
                    methods: {
                        submitForm: function () {
                                var $this = this;
                                var FormData =this.form;
                                    //写法1
                                    axios.post(
                                        '{:Url('login')}',
                                        {   //使用post 数据请求,其他请求方法见官方demo
                                            'key': FormData.key,
                                            'username': FormData.username,
                                            'password': FormData.password,
                                        }).then(function (res) {
                                            console.log(res);   //成功数据返回
                                        }).catch(function (err) {
                                            console.log(err);   //失败数据返回
                                    });
                                 
                                    //写法2
                                    axios({
                                        method: 'post',     //提交方法
                                        url: '{:Url('login')}',
                                        data: {
                                            firstName: 'Fred',
                                            lastName:  'Flintstone'
                                        }
                                    })
                                    .then(function (response) {
                                        console.log(response);  //成功数据返回
                                    })
                                    .catch(function (error) {
                                        console.log(error);     //失败数据返回
                                    }); 
                            }
                    }
                })
            };
        </script>
        

2.1、axios 使用关联 vue 当中的方法


        <form id="login">
            <input name="username" type="text"      placeholder="用户名"    v-model="form.username" >
            <input name="password" type="password"  placeholder="密 码"    v-model="form.password" >
            <input name="key"      type="text"      placeholder="验证码"     v-model="form.key">
            <input type="button"   @click="submitForm()"    value="登 录">
            <Modal v-model="visible" title="Welcome">Welcome to iView</Modal>
        </form>
        <!---加载vue.js 开发版本--->
        <script src="__PLUGS__vue.js"></script>
        <!---加载axios.js 库--->
        <script src="__PLUGS__axios.min.js"></script>
        <script>
            window.onload = function () {
                const app = new Vue({
                    el: '#login',   //对应使用id="app"获取信息。
                    data: {
                        form:{
                            username: '',
                            password: '',
                            key: ''
                        },
                    },
                    methods: {
                        submitForm: function () {
                            var $this = this;
                            var FormData =this.form;
                                //写法1
                                axios.post(
                                    '{:Url('login')}',
                                    {   //使用post 数据请求,其他请求方法见官方demo
                                        'key': FormData.key,
                                        'username': FormData.username,
                                        'password': FormData.password,
                                    }).then(res =>{         //继承vue 类
                                        console.log(1111);  //成功数据返回
                                        console.log(res);   //成功数据返回
                                        this.show(res);     //调用vue 方法
                                    }).catch(function (err) {
                                        console.log(2222);  //失败数据返回
                                        console.log(err);   //失败数据返回
                                }); 
                        },
                        show: function (){
                             this.visible = true;
                        }   
                            
                    }
                })
            };
        </script>
        

3.0、 使用require.js模块化

3.1、设置初始化app.js(命名为自定config.js或其他名字都可以)。
RequireJS,发现他的缓存十分严重,所以需要加urlArgs

requirejs.config({
    urlArgs: "v=15615616515616556",
    map: {
        '*': {
            'css': '/static/plugs/require/require-css.js'   //非AMD模块 css输出所需加载的类库
        }
    },
    shim:{
        'iview':{
            deps:['css!iviewcss'] //iview.css必须在iview/dist/styles/的文件夹下面,不然对应字体文件路径不匹配
        },
    },
    paths: {    //以字段代替对应文件路径
        'static':'static',
        'system':'system',
        'vue':'/static/plugs/vue/dist/vue.min',
        'axios':'/static/plugs/axios/dist/axios.min',
        'iview':'/static/plugs/iview/dist/iview.min',
        'iviewcss':'/static/plugs/iview/dist/styles/iview',
        'jquery':'/static/plugs/jquery/jquery-3.4.1',
    },
});

如果使用data-main="__PLUGS__require/app.js" 加载配置的方式会出现所对应文件丢失的现象。也就是偶尔能加载到文件,偶尔加载不到。所以,配置文件应该放在当前页面之下。

<script>
    
    requirejs.config({
        urlArgs: "v=15615616515616556",
        map: {
            '*': {
                'css': '/static/plugs/requirejs/require-css.js'
            }
        },
        //baseUrl:'/static/plugs/',
        shim:{
            'iview':{
                deps:['css!iviewcss'] //iview.css必须在iview/dist/styles/的文件夹下面,不然对应字体文件路径不匹配
            },
        },
        paths: {    //以字段代替对应文件路径
            'static':'static',
            'system':'system',
            'vue':'/static/plugs/vue/dist/vue.min',
            'axios':'/static/plugs/axios/dist/axios.min',
            'iview':'/static/plugs/iview/dist/iview.min',
            'iviewcss':'/static/plugs/iview/dist/styles/iview',
        },
    });
</script>
    
<script>
    window.onload = function(){
        
        
        require(['vue','iview','axios'], function (Vue,iview,axios) {
            Vue.use(iview); 
            const app = new Vue({
                el: '#login',   //对应使用id="app"获取信息。
                data: {
                    form:{
                        username: '',
                        password: '',
                        key: '',
                        //visible: false
                    },
                    visible1:false,
                    visible2:false,
                    visible3:false
                },  
                methods: {
                    submitForm: function () {
                            var $this = this;
                            var FormData =this.form;
                                //写法1
                                axios.post(
                                    '{:Url('login')}',
                                    {   //使用post 数据请求,其他请求方法见官方demo
                                        'key': FormData.key,
                                        'username': FormData.username,
                                        'password': FormData.password,
                                    }).then(res =>{         //继承vue 类
                                        console.log(1111);  //成功数据返回
                                        console.log(res);   //成功数据返回
                                        this.show(res);     //调用vue 方法
                                    }).catch(function (err) {
                                        console.log(2222);  //失败数据返回
                                        console.log(err);   //失败数据返回
                                }); 
                        
                    },
                    show: function (){
                        this.visible = true;
                        this.$Modal.info({
                            title: title,
                            content: content
                        });
                        
                    }  
                }
            });
            

        })
    }   
</script>


3.2、页面使用

<form id="login">
            <input name="username" type="text"      placeholder="用户名"    v-model="form.username" >
            <input name="password" type="password"  placeholder="密 码"    v-model="form.password" >
            <input name="key"      type="text"      placeholder="验证码"     v-model="form.key">
            <input type="button"   @click="submitForm()"    value="登 录">
            <Modal v-model="visible" title="Welcome">Welcome to iView</Modal>
        </form>
        <script data-main="__PLUGS__require/app.js" src="__PLUGS__require/require.js"></script>
        <script>
        window.onload = function () {
            require(['vue','iview','axios','jquery'], function (Vue,iview,axios,$) {
                //require 是文件路径;function是使用方法别名,可自取.
                Vue.use(iview); //vue 内关联iview 
                const app = new Vue({
                    el: '#login',   //对应使用id="app"获取信息。
                    data: {
                        form:{
                            username: '',
                            password: '',
                            key: ''
                        },
                    },
                    methods: {
                        submitForm: function () {
                            var $this = this;
                            var FormData =this.form;
                                //写法1
                                axios.post(
                                    '{:Url('login')}',
                                    {   //使用post 数据请求,其他请求方法见官方demo
                                        'key': FormData.key,
                                        'username': FormData.username,
                                        'password': FormData.password,
                                    }).then(res =>{         //继承vue 类
                                        console.log(1111);  //成功数据返回
                                        console.log(res);   //成功数据返回
                                        this.show(res);     //调用vue 方法
                                    }).catch(function (err) {
                                        console.log(2222);  //失败数据返回
                                        console.log(err);   //失败数据返回
                                }); 
                        },
                        show: function (){
                             this.visible = true;
                        }   
                            
                    }
                })
            }
        };
        </script>       

后台登陆实例

    <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>网站后台管理</title>
<link href="__LOGIN__css/master.css" rel="stylesheet" type="text/css">
<script>
function edoshowkey(showid,vname){
    document.getElementById(showid).innerHTML='<img src="{:url(\'/admin/login/captcha\')}?v='+vname+'&t='+Math.random()+'" name="'+vname+'KeyImg" id="'+vname+'KeyImg" align="bottom" onclick=edoshowkey("'+showid+'","'+vname+'") title="看不清楚,点击刷新">';
}
</script>
</head>
<body>
<div class="main">
    <form  id="login"  autocomplete="off">
        <div class="logo"><img src="__LOGIN__images/loginlogo.png"></div>
        <div class="login">
            <div class="bd">
                <img src="__LOGIN__images/ico1.png" width="20" height="20">
                <input v-model="form.username" name="username" type="text" class="form-control"   placeholder="用户名">
            </div>
            <div class="bd">
                <img src="__LOGIN__images/ico2.png" width="19" height="21">
                <input v-model="form.password" name="password" type="password" class="form-control"  placeholder="密 码">
            </div>
            <div>
                <img src="__LOGIN__images/ico3.png" width="21" height="19">
                <input v-model="form.key" name="key" type="text" placeholder="验证码" class="form-control">
                <span id="checkkeyshowkey">
                    <a href="javascript:;" onClick="edoshowkey('checkkeyshowkey','checkkey');" title="点击显示验证码">点击显示</a>
                </span>
            </div>
        </div>
        <div class="denglu">
            <input type="button" class="regsubmit"   @click="submitForm()"    value="登 录">
        </div>


        <div class="jszc">
        Copyright &copy;2019-2024 <a href="https://www.ulimeco.com" target="_blank"> www.ulimeco.com</a>,All Rights Reserved.
        </div>
    </form>
</div>
<script  src="__PLUGS__requirejs/require.js"></script>



<script>

    requirejs.config({
        urlArgs: "v=15615616515616556",
        map: {
            '*': {
                'css': '/static/plugs/requirejs/require-css.js'
            }
        },
        //baseUrl:'/static/plugs/',
        shim:{
            'iview':{
                deps:['css!iviewcss'] //iview.css必须在iview/dist/styles/的文件夹下面,不然对应字体文件路径不匹配
            },
        },
        paths: {    //以字段代替对应文件路径
            'static':'static',
            'system':'system',
            'vue':'/static/plugs/vue/dist/vue.min',
            'axios':'/static/plugs/axios/dist/axios.min',
            'iview':'/static/plugs/iview/dist/iview.min',
            'router':'/static/plugs/vue-router/dist/vue-router.min',
            'iviewcss':'/static/plugs/iview/dist/styles/iview',
        },
    });
</script>
<script>
    window.onload = function(){
        require(['vue','iview','axios','router'], function (Vue,iview,axios,router){
            Vue.use(iview);
            Vue.use(router);
            const app = new Vue({
                el: '#login',   //对应使用id="app"获取信息。
                data: {
                    form:{
                        username: '',
                        password: '',
                        key: '',
                    },
                },
                methods: {
                    submitForm: function () {
                        var $this = this;
                        var FormData =this.form;
                        //写法1
                        axios.post(
                            '{:Url('verify')}',
                            {   //使用post 数据请求,其他请求方法见官方demo
                                'key': FormData.key,
                                'username': FormData.username,
                                'password': FormData.password,
                            }
                        ).then(res =>{         
                            //console.log(res);   //成功数据返回
                            if(res.data.code == 1){
                                window.location.href=res.data.data;
                            }else{
                                this.warning(res.data.msg);     //调用vue 方法
                            }
                        }).catch(function (err) {
                                //console.log(err);   //失败数据返回
                                this.error(err.data.msg);
                        });
                    },
                    success (msg) {
                        //console.log(msg);
                        this.$Message.success(msg);
                    },
                    warning (msg) {
                        //console.log(msg);
                        this.$Message.warning(msg);
                    },
                    error (msg) {
                        //console.log(msg);
                        this.$Message.error(msg);
                    },
                }
            });


        })
    }
</script>



</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,366评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,521评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,689评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,925评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,942评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,727评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,447评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,349评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,820评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,990评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,127评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,812评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,471评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,017评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,142评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,388评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,066评论 2 355

推荐阅读更多精彩内容

  • 基于Vue的一些资料 内容 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 element★...
    尝了又尝阅读 1,154评论 0 1
  • UI组件 element- 饿了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的组件库 m...
    小姜先森o0O阅读 9,495评论 0 72
  • UI组件 element- 饿了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的组件库 m...
    柴东啊阅读 15,857评论 2 140
  • UI组件 element- 饿了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的组件库 m...
    你猜_3214阅读 11,069评论 0 118
  • UI组件 element- 饿了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的组件库 m...
    王喂马_阅读 6,455评论 1 77