好家伙,本篇用于測驗"添加"介面,為后續"用戶注冊"功能做鋪墊
(完整代碼在最后)
我們要實作"添加"功能
老樣子我們先來理清一下思路,
現在,我是一個用戶,我來到了注冊頁面,我在一個①表單中要把我要添加的資料填好,
然后點擊添加按鈕,進行②資料校驗(看看用戶有沒有少填的資料),
隨后,③發送網路請求,把資料傳到后端,后端再把資料傳到資料庫中,
最后,④重置表單
大概是這么回事
老樣子,我們先去餓了么偷點東西

拿個表單,然后該刪的刪,最后留下序號,書名,作者三行以及兩個按鈕就好
1.前端基本視圖

代碼如下:
<template> <el-form style="width: 60%" :model="ruleForm" :rules="rules" ref="ruleForm" label- ><el-form-item label="圖書編號" prop="id"> <el-input v-model="ruleForm.id"></el-input> </el-form-item>
<el-form-item label="圖書名稱" prop="name"> <el-input v-model="ruleForm.name"></el-input> </el-form-item>
<el-form-item label="作者" prop="author"> <el-input v-model="ruleForm.author"></el-input> </el-form-item>
<el-form-item> <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button> <el-button @click="resetForm('ruleForm')">重置</el-button> </el-form-item>
</el-form> </template>
2.資料校驗方法
驗證規則如下
rules: {
id:[
{ required: true, message: 'ID不能為空', trigger: 'blur' }
],
name: [
{ required: true, message: '圖書名稱不能為空', trigger: 'blur' }
],
author:[
{ required: true, message: '作者不能為空', trigger: 'blur' }
]
}
2.1.
required: true,
不可為空
2.2.
trigger: 'blur'
失去焦點時進行校驗
3.點擊"添加"",發起網路請求
submitForm(formName) {
const _this = this
this.$refs[formName].validate((valid) => {
if (valid) {
axios.put('XXX/XXX/XXX',this.ruleForm).then(function(resp){
if(resp.data =https://www.cnblogs.com/FatTiger4399/archive/2022/10/27/= 'success'){
_this.$alert('《'+_this.ruleForm.name+'》修改成功!', '訊息', {
confirmButtonText: '確定',
callback: action => {
_this.$router.push('/....')
}
})
}
})
} else {
return false;
}
});
},
3.1.validate()方法
說明:這是一個校驗方法,vue組件庫element-ui 的validate方法_moni110的博客-CSDN博客_elementui validate
validate()時elment-ui封裝好的用于對整個表單進行驗證
somethig.validate((valid) => {
if{valid}()
}
其中的valid是布林值,為true時表示校驗通過,
呼叫validate()校驗時,需要prop屬性系結校驗項的欄位名
如果prop屬性不系結name這個欄位名,表單校驗時,不會校驗變數ruleForm.name 的值,
3.2.resp接受"網路請求"的回傳值
成功了就談個窗,告訴你成功了
失敗了就,...就寄
4.重置表單方法:
resetForm(formName) {
this.$refs[formName].resetFields();
}
4.1.
resetFields();
這是組件里面官方的表單重置方法,(以前用這個方法次次寄,這次居然能用了)
5.該部分完整代碼如下:
<template>
<el-form style="width: 60%" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
<el-form-item label="圖書編號" prop="id">
<el-input v-model="ruleForm.id"></el-input>
</el-form-item>
<el-form-item label="圖書名稱" prop="name">
<el-input v-model="ruleForm.name"></el-input>
</el-form-item>
<el-form-item label="作者" prop="author">
<el-input v-model="ruleForm.author"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
<el-button @click="resetForm('ruleForm')">重置</el-button>
</el-form-item>
</el-form>
</template>
<script>
export default {
data() {
return {
ruleForm: {
id: '',
name: '',
author: ''
},
rules: {
name: [
{ required: true, message: '圖書名稱不能為空', trigger: 'blur' }
],
author:[
{ required: true, message: '作者不能為空', trigger: 'blur' }
]
}
};
},
methods: {
submitForm(formName) {
const _this = this
this.$refs[formName].validate((valid) => {
if (valid) {
axios.put('http://localhost:8181/book/update',this.ruleForm).then(function(resp){
if(resp.data =https://www.cnblogs.com/FatTiger4399/archive/2022/10/27/= 'success'){
_this.$alert('《'+_this.ruleForm.name+'》修改成功!', '訊息', {
confirmButtonText: '確定',
callback: action => {
_this.$router.push('/BookManage')
}
})
}
})
} else {
return false;
}
});
},
resetForm(formName) {
this.$refs[formName].resetFields();
}
},
}
</script>
后端介面還沒寫好,效果預覽圖暫無
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/521891.html
標籤:其他
上一篇:如何用webgl(three.js)搭建一個3D庫房,3D倉庫3D碼頭,3D集裝箱,車輛定位,叉車定位可視化孿生系統——第十五課
