您好,登錄后才能下訂單哦!
這篇文章主要介紹ElementUI中如如何實現表單驗證、數據綁定、路由跳轉,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
1.新建表單組件el-form.vue
<template> <div class="form"> <el-form ref="form" :model="UserForm" :rules="this.$store.state.userinfo.CheckRules" label-position="left" label-width="180px"> <el-form-item label="真實姓名" prop="truename"> <el-input v-model="UserForm.truename"></el-input> </el-form-item> <el-form-item label="年齡" prop="age"> <el-input v-model="UserForm.age"></el-input> </el-form-item> <el-form-item label="出生年月"> <el-date-picker type="date" placeholder="選擇日期" v-model="UserForm.birthday"> </el-date-picker> </el-form-item> <el-form-item> <el-button type="primary" @click="submitInfo">提交</el-button> </el-form-item> </el-form> </div> </template> <style> .form{margin-top: 20px;} </style> <script> export default { created(){ this.UserForm = this.$store.state.userinfo.UserForm; }, beforeDestory(){ this.$store.state.userinfo.UserForm=this.UserForm; }, data() { return { UserForm: { truename: '', age: '', birthday: '' } }; }, methods:{ submitInfo(){ // 表單驗證方法 this.$refs.form.validate(function (result) { if(result){ // 驗證通過,調用module里的setUserInfo方法 this.$store.dispatch("setUserInfo"); }else{ alert("表達驗證不合法") } }.bind(this)); } }, } </script>
2.新建一個Module,EleUserInfo.js
import Vue from "vue"; import {routerConfig} from "./../../jssrc/config"; export default{ state:{ UserForm:{truename:"", age:"", birthday:""}, CheckRules:{ truename:[ {required:true, message:"請輸入活動名稱", trigger:'blur'}, {min:3, max:5, message:"必須3-5個字符", trigger:'blur'} ], age:[{validator:(rule,value,callback)=>{ if(/^\d+$/.test(value) == false){ callback(new Error("年齡只能輸入數字")); }else{ callback(); } }, trigger:'blur'}] }, }, actions:{ setUserInfo(context){ // 路由跳轉 routerConfig.push({name:"newslist"}); } } }
3.我們把jssrc/index.js 中的代碼分離出一部分
config.js
import VueRouter from 'vue-router'; import newsdetail from "./../components/news-detail.vue"; import eltable from "./../components/el-table.vue"; import elindex from "./../components/elmentui/el-index.vue"; import elhot from "./../components/elmentui/el-hot.vue"; import eluserinfo from "./../components/elmentui/el-form.vue"; const userlogin = resolve => { // 成功之后的回調 resolve(require("./../components/user-login.vue")); // 這就是異步加載的方式 } const newslist = resolve => { // 成功之后的回調 resolve(require("./../components/news-list.vue")); // 這就是異步加載的方式 } // 路由模塊 export const routerConfig = new VueRouter({ routes: [ { path: '/', component: userlogin}, { path: '/news', component: newslist, name:"newslist"}, { path: '/news/:newsid', component: newsdetail, name:"newsdetail"}, { path: '/login', component: userlogin,name:"userlogin" }, { path: '/eltable', component: eltable,name:"eltable" }, { path: '/elindex', component: elindex,name:"elindex", children:[ {path:"hot",component:elhot,name:"elhot"}, {path:"userinfo",component:eluserinfo,name:"eluserinfo"}, ]} ] })
4.然后jssrc/index.js 修改成如下
import Vue from "vue"; import pagenav from "./../components/page-nav.vue"; // 引入 import validate from "./../components/validate"; // 使用 Vue.use(validate); import VueRouter from 'vue-router'; Vue.use(VueRouter); // 全局使用插件 import VueResource from 'vue-resource'; Vue.use(VueResource); import UserModule from "./../store/modules/UserModule"; import NewsModule from "./../store/modules/NewsModule"; import EleModule from "./../store/modules/eleModule"; import EleUserInfomoldule from "./../store/modules/EleUserInfo"; import Vuex from 'vuex'; Vue.use(Vuex); import ElementUI from 'element-ui'; import 'element-ui/lib/theme-default/index.css'; Vue.use(ElementUI); const vuex_store = new Vuex.Store({ modules:{ users:UserModule, news:NewsModule, ele:EleModule, userinfo:EleUserInfomoldule } }) //路由的配置 import {routerConfig} from "./config"; // 全局組件,不加入路由管理 Vue.component("page-nav",pagenav); let myvue = new Vue({ el:".container", store:vuex_store, //注入到vue router:routerConfig, });
5.查看效果
以上是“ElementUI中如如何實現表單驗證、數據綁定、路由跳轉”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。