前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >vue使用 Object.assign()巧妙重置data数据

vue使用 Object.assign()巧妙重置data数据

作者头像
不爱吃糖的程序媛
发布2024-01-18 21:09:22
4380
发布2024-01-18 21:09:22
举报

Object.assign()的用法

Object.assign方法用于对象的合并,将源对象(source)的所有可枚举属性,复制到目标对象(target)。 Object.assign方法的第一个参数是目标对象,后面的参数都是源对象。

代码语言:javascript
复制
const target = { a: 1 };

const source1 = { b: 2 };
const source2 = { c: 3 };

Object.assign(target, source1, source2);
target // {a:1, b:2, c:3}
浅拷贝

Object.assign方法实行的是浅拷贝,而不是深拷贝。也就是说,如果源对象某个属性的值是对象,那么目标对象拷贝得到的是这个对象的引用。

由于Object.assign()有上述特性,因此Vue组件需要重置Vue组件的data数据的需求时,我们可以这么用

源代码
代码语言:javascript
复制
 methods: {
    handleClose() {
      //通过this.$data获取当前状态下的data,通过this.$options.data()获取该组件初始状态下的data。
      //然后只要使用Object.assign(this.$data, this.$options.data())就可以将当前状态的data重置为初始状态
      Object.assign(this.$data, this.$options.data.call(this));//加这句话就可以清空
      this.$refs.forms.resetFields();
      this.$emit("resetGoodsInfo");
      this.$emit("handleClose");
    },
}
代码语言:javascript
复制
<template>
  <Dialog
    :title="$t('lang.leaveWarehouse')"
    :visible="visible"
    width="800px"
    :loading="loading"
    :btnName="$t('lang.submit')"
    @handleOk="handleOk"
    @handleClose="handleClose"
  >
    <div class="dialog-content">
      <el-form
        :model="ruleForm"
        :rules="rules"
        :validate-on-rule-change="false"
        ref="forms"
        label-width="120px"
        label-position="right"
      >
        <el-form-item :label="$t('lang.cargoName')" prop="goodsSn">
          <el-select
            v-model="ruleForm.goodsSn"
            filterable
            :placeholder="$t('lang.pleaseSelect')"
            :disabled="isCertainItem"
            @change="handleSelectGoods"
          >
            <el-option
              :label="
                item.goodsName.length > 40
                  ? item.goodsName.slice(0, 40) + '...'
                  : item.goodsName
              "
              :value="item.goodsSn"
              v-for="(item, index) in cargoList"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('lang.specification')" prop="specificationId">
          <el-select
            v-model="ruleForm.specificationId"
            :placeholder="$t('lang.pleaseSelect')"
            :disabled="specificationList.length === 0 || isCertainItem"
            @change="handleSpecificationGoods"
          >
            <el-option
              :label="item.specification"
              :value="item.id"
              v-for="(item, index) in specificationList"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('lang.currentStockLocation')" prop="shelvesSn">
          <div class="current-stock-locate">
            <div v-for="(item, index) in shelvesList" :key="index">
              <div
                class="location-item"
                :class="isSelectShow && mouseIndex == index ? 'clicked' : ''"
                @click="
                  selectCurrentLocation(
                    item.shelvesSn,
                    item.shelvesLevel,
                    item.id,
                    item.goodsCount,
                    item.goodsUnit,
                    index
                  )
                "
              >
                {{
                  `${item.shelvesSn} ${$t("lang.nth")}${item.shelvesLevel}${$t(
                    "lang.level"
                  )} ${item.goodsCount}${item.goodsUnit}`
                }}
              </div>
            </div>
          </div>
        </el-form-item>
        <el-form-item :label="$t('lang.leaveStockAmount')" prop="goodsCount">
          <div class="amount-unit">
            <el-input
              v-model.trim="ruleForm.goodsCount"
              :placeholder="$t('lang.pleaseEnter')"
              @blur="resetGoodsCount(ruleForm.goodsCount)"
            >
              <template slot="append">
                <div class="number-calc-icon">
                  <em
                    class="el-icon-caret-top"
                    @click="addNum(ruleForm.goodsCount)"
                  ></em>
                  <em
                    class="el-icon-caret-bottom"
                    @click="reduceNum(ruleForm.goodsCount)"
                  ></em>
                </div>
              </template>
            </el-input>
            <div class="unit">{{ goodsUnit }}</div>
          </div>
        </el-form-item>
         <el-form-item :label="$t('lang.claimant')" prop="checker">
          <el-input
            :rows="3"
            :maxlength="50"
            v-model="ruleForm.claimant"
            :placeholder="$t('lang.pleaseEnterClaimant')"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('lang.remark')" prop="desc">
          <el-input
            type="textarea"
            :rows="3"
            :maxlength="200"
            v-model="ruleForm.recordDesc"
            :placeholder="$t('lang.pleaseEnter')"
          ></el-input>
        </el-form-item>
         <el-form-item prop="operateType">
           <el-checkbox v-model="checked" @change="changeCheckbox()" :label="$t('lang.returnSupplier')"></el-checkbox>
         </el-form-item>
      </el-form>
    </div>
  </Dialog>
</template>
注意:

data()中若使用了this来访问props或methods,在重置

data时,注意this.

options.data()的this指向,最好使用this.$options.data.call(this)。

https://blog.csdn.net/mocoe/article/details/89682022?

效果图:

填入信息:

在这里插入图片描述
在这里插入图片描述

关闭后再次打开:

在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-02-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Object.assign()的用法
    • 浅拷贝
      • 源代码
        • 注意:
          • 效果图:
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档