其他分享
首页 > 其他分享> > vue 输入框失焦时做本地存储

vue 输入框失焦时做本地存储

作者:互联网

<van-field
  v-model="phone"
  name="phone"
  type="digit"
  maxlength="13"
  input-align="right"
  placeholder="请输入11位数字"
  label="手机号"
  @blur="setLocalStorage" />
created () {
	let data = window.localStorage.getItem('financialData')
	if (data) {
	     data = JSON.parse(data)
	     this.nickname = data.nickname
	     this.phone = data.phone
	     this.sex = data.sex
	   }
	}
methods: {
setLocalStorage () {
   const newData = {
     nickname: this.nickname,
     phone: this.phone,
     sex: this.sex
   }
   let data = window.localStorage.getItem('financialData')
   if (!data) {
     data = newData
   } else {
     data = Object.assign(JSON.parse(data), newData) //form表单提交转json
   }
   try {
     window.localStorage.setItem('financialData', JSON.stringify(data))
   } catch (error) {
   }
 }
}

标签:vue,phone,nickname,JSON,sex,输入框,window,失焦,data
来源: https://blog.csdn.net/weixin_43548815/article/details/121253877