var form = Object.assign({},this.form);
时间: 2024-05-10 21:06:02 浏览: 16
这段代码使用了 JavaScript 中的 Object.assign() 方法,它的作用是将一个或多个源对象的属性复制到目标对象中,然后返回目标对象。在这里,目标对象是一个空对象,由 {} 表示,源对象是 this.form。通过这种方式,我们可以克隆一个对象,而不会影响原始对象。这在很多场景中很有用,比如在修改表单数据时,我们可以克隆表单数据对象,然后对克隆的对象进行修改,而不会对原始的表单数据对象造成影响。
相关问题
export default { mixins: [mixin], components: {}, data() { return { url_submit: "~/api/user/change_password?", field: "user_id", query: {}, form: { "o_password": "", "password": '', }, confirm_password: '' } }, methods: { get_form() { var _this = this; this.$get(this.url_get_obj, null, function(res) { if (res.result && res.result.obj) { $.push(_this.form, res.result.obj); } }); }, submit_before(param){ var p = Object.assign({},param); return p; }, submit_check(param) { if (this.form.password !== this.confirm_password) { return "密码和确认密码不一致!" } return null; }, submit_after(res, status) { this.$store.commit("quit"); this.$router.push("/login"); } }, created() { this.form.username = this.user.username; this.get_form(); } }
这是一个 Vue.js 的组件定义,其中包含了一个名为 `change_password` 的组件。该组件使用了一个名为 `mixin` 的 mixin,并定义了一个包含以下属性的 data 对象:
- `url_submit`:表示提交表单时的请求路径
- `field`:表示提交表单时需要携带的字段
- `query`:表示提交表单时需要携带的查询参数
- `form`:表示表单数据对象,包含了 `o_password` 和 `password` 两个属性
- `confirm_password`:表示确认密码输入框的值
该组件还定义了以下方法:
- `get_form`:发送一个 GET 请求,获取当前用户的数据,并将其填充到表单中
- `submit_before`:在提交表单前对表单数据进行处理的方法
- `submit_check`:在提交表单前对表单数据进行校验的方法,如果校验不通过,返回错误信息
- `submit_after`:在提交表单成功后的回调函数,该函数会将用户退出,并跳转到登录页面
在 `created` 钩子函数中,该组件会为表单的 `username` 属性赋值为当前用户的用户名,并调用 `get_form` 方法来初始化表单数据。
解释代码formSubmit: function () { var that = this; //如果openid不存在,就重新请求接口获取openid var openid = app.globalData.openid; if (openid === null || openid === undefined) { app.getOpenid(); wx.showToast({ //这里提示失败原因 title: 'openid为空!', duration: 1500 }) return; } let username = that.data.username; let phone = that.data.phone; let zhuohao = that.data.zhuohao; let renshu = that.data.renshu; let first = that.data.first; if (username == '' || username == undefined) { wx.showToast({ title: '用户名不能为空', icon: 'none' }) return; } if (phone == '' || phone == undefined) { wx.showToast({ title: '手机号不能为空', icon: 'none' }) return; } wx.request({ url: app.globalData.baseUrl + '/user/save', method: "POST", header: { "Content-Type": "application/x-www-form-urlencoded" }, data: { openid: openid, username: username, phone: phone, zhuohao: zhuohao == undefined ? '' : zhuohao, renshu: renshu == undefined ? '' : renshu, money: first ? 1000 : 0 //第一次注册送1000积分 }, success(res) { if (res && res.data && res.data.data) { console.log("修改成功", res.data) wx.showToast({ title: '修改成功', }) let user = app.globalData.vipInfo Object.assign(user, res.data.data) app.globalData.vipInfo = user app._getMyUserInfo(); } // wx.navigateBack(); wx.switchTab({ url: '/pages/me/me' }) }, fail(res) { console.log("提交失败", res) } }) },
这是一个小程序的表单提交函数,主要的逻辑如下:
1. 首先获取全局变量中的openid,如果不存在则调用app.getOpenid()获取openid,并且弹出一个提示框。
2. 获取表单中的用户名、手机号、桌号、人数和第一次注册标志。
3. 对于用户名和手机号进行非空判断,如果为空则弹出一个提示框并且返回。
4. 使用wx.request()函数向后端发送一个POST请求,请求的URL是全局变量app.globalData.baseUrl + '/user/save',请求头中Content-Type是"application/x-www-form-urlencoded"。
5. 请求体中包含openid、用户名、手机号、桌号、人数和第一次注册送积分的标志。
6. 如果请求成功,则将返回的数据合并到全局变量app.globalData.vipInfo中,并且调用app._getMyUserInfo()函数更新用户信息,然后跳转到“我”的页面。
7. 如果请求失败,则在控制台打印“提交失败”。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)