背景
实现注册功能,控制台报错:
Cannot destructure property ‘username’ of ‘undefined’ as it is undefined
原因
不知道
导出发请求的函数时使用解构赋值携带了未定义的参数
// src\api\index.jsexport const register = ({ username, password, repassword }) => {return request({url: '/api/reg',method: 'POST',data: {username: username,password: password,repassword: repassword}})}
// src\views\register\index.vueregisterFn () {this.$refs.form.validate(async valid => {if (valid) {const res = await register(this.form)}})}
解决
在页面处处理解构
// src\api\index.jsexport const register = (data) => {return request({url: '/api/reg',method: 'POST',data})}
// src\views\register\index.vueregisterFn () {const { username, password, repassword } = this.formthis.$refs.form.validate(async valid => {if (valid) {const res = await register({ username, password, repassword }) } }