以下寫一個簡單的From提交
**注 使用表單驗證 form-item 必須添加 prop ='xxxx' **
~~~
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>iview example</title>
<link rel="stylesheet" type="text/css" href="//unpkg.com/iview/dist/styles/iview.css">
<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/iview/dist/iview.min.js"></script>
<script src="//unpkg.com/axios/dist/axios.min.js"></script>
<script src="//cdn.bootcss.com/qs/6.5.1/qs.js"></script>
</head>
<body>
<div id="app">
<!--ref=userForm ref指子組件; model="formData" 綁定formData 提交表單用到的; rules 驗證規則-->
<i-form ref="userForm" :model="formData" :rules="ruleInline">
<form-item prop="user">
<!--v-model="formData.user" 進行數據綁定-->
<i-input type="text" v-model="formData.user" placeholder="用戶名">
<icon type="ios-person-outline" slot="prepend"></icon>
</i-input>
</form-item>
<form-item prop="password">
<i-input type="password" v-model="formData.password" placeholder="用戶密碼">
<icon type="ios-locked-outline" slot="prepend"></icon>
</i-input>
</form-item>
<form-item>
<!--@click="handleSubmit('userForm')" 點擊按鈕觸發事件并傳入要驗證的form名-->
<i-button type="primary" @click="handleSubmit('userForm')">添加</i-button>
</form-item>
</i-form>
</div>
<script>
var Main = {
data() {
return {
// 相關數據配置跟規則配置
formData: {
user: '',
password: ''
},
ruleInline: {
user: [
// 配置是否驗證, 錯誤提示信息, 觸發驗證場景
{ required: true, message: '請輸入用戶名稱.', trigger: 'blur' }
],
password: [
{ required: true, message: '請輸入用戶密碼..', trigger: 'blur' },
{ type: 'string', min: 6, message: '最少6位密碼', trigger: 'blur' }
]
}
}
},
methods: {
handleSubmit(name) {
// 驗證是否通過規則
this.$refs[name].validate((valid) => {
if (!valid) {
this.$Message.error('驗證失敗!');
return;
}
})
// 發送到接口
var postData = Qs.stringify(this.formData); // 必須用到的一個關鍵點, 把JSON數據轉成 后端能接收到的 FORM-DATA
axios.post("{:url('user')}", postData).then((res) => {
var data = res.data // 這個是插件獲取后后端的返回數據 記得是.data
if (!res.data.status) {
this.$Message.error(data.msg);
} else {
this.$Message.success(data.msg);
}
})
}
}
}
var Component = Vue.extend(Main)
new Component().$mount('#app')
</script>
</body>
</html>
~~~
接收 后端代碼
~~~
<?php
namespace app\index\controller;
use think\Controller;
use app\index\model\User;
class Index extends Controller
{
protected $request;
protected $params;
public function __construct()
{
parent::__construct();
$this->request = request();
$this->params = $this->request->param();
}
public function index()
{
return $this->fetch();
}
/**
* 添加用戶
*/
public function user(User $user)
{
try {
$user->saveUser($this->params);
} catch(\Exception $e) {
return json([
'status' => 0,
'msg' => $e->getMessage(),
]);
}
return json([
'status' => 1,
'msg' => '添加成功'
]);
}
}
~~~
- 介紹
- EasyWechat3
- 安裝篇
- 吃上Easywechat--獲取用戶信息篇
- 吃上Easywechat--模板消息篇
- 吃上Easywechat--支付篇
- Easywechat4
- 安裝與接入
- 公眾號
- 網頁授權
- JSSDK
- 發送模板消息
- 自定義菜單
- 消息類型對應處理
- 微信支付
- 統一下單
- 支付通知
- 接入企業號
- 開發福利
- 福利篇--內透
- 福利篇--微信遠程調試
- 隊列
- 隊列搭配Supervisor
- 隊列搭配Supervisor--多進程
- think隊列微信通知
- 隊列執行情況查詢與反饋
- 模型使用進價
- 模型,拋出異常,減少if的使用
- 多使用模型,好處還是有的
- 優化建議
- 多條件搜索代碼優化(閉包查詢)
- 使用group & 子查詢進行統計
- 雜類
- 寫Composer包
- PHP單例
- PPT轉圖片
- 個人開發環境
- Xdebug配置
- emoji表情
- 后端玩iView
- 準備篇
- 列表List--普通篇
- 表單Form篇
- 列表List--異步篇
- 開發小技巧
- 時間戳(日期)統計
- API版本
- 技術群
- Java小筆記
- WebSocket
- 跨域配置
- Nginx配置(ssl 強制ssl反向代理)
- Json信息 response返回