vue 中数组中的某个对象的属性发生变化,视图不更新如何解决

问题描述:

vue中js修改对象中的属性或修改数组对象中的属性时视图不更新

解决方法:

语法 this.$set(对象, '属性', 重新值)

用法 this.$set( target, key, value )

         target:要更改的数据源(可以是对象或者数组)
         key:要更改的具体数据
         value :重新值

实际场景例子: 

  <el-col :span="24">
              <el-form-item label="缴费日期" prop="chargeTime">
                <el-date-picker
                  v-model="model.chargeTime"
                  type="date"
                  placeholder="选择日期"
                  :picker-options="expireTimeOption"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item>
                <el-button
                  :autofocus="false"
                  :class="num == 1 ? 'Cclass' : 'CClass'"
                  @click="toDay(1)"
                  >今天</el-button
                >
                <el-button
                  :autofocus="false"
                  :class="num == 2 ? 'Cclass' : 'CClass'"
                  @click="yesterDay(2)"
                  >昨天</el-button
                >
                <el-button
                  :autofocus="false"
                  :class="num == 3 ? 'Cclass' : 'CClass'"
                  @click="beforeYesterday(3)"
                  >前天</el-button
                >
              </el-form-item>
     </el-col>
<script>

export default {
  data() {
    // 当天日期之后不能选择的事件
    expireTimeOption: {
          disabledDate(time) {
            return time.getTime() > new Date();
          },
        },
     },
    return {
      // 缴费日期
       chargeTime: [],
       num: 0,
       rules: {
           chargeTime: [
                {
              required: true,
              message: '请录入实际收费日期',
                },
            ],
           }
       };
  },
  
  methods: {
       // 把时间 2021-10-31T05:48:21.957Z 格式转化为 2021-10-11 10:00:00
      const date = new Date(this.model.chargeTime);
          let Y = date.getFullYear();
          let M = date.getMonth() + 1;
          let D = date.getDate();
          if (M < 10) {
            M = '0' + M;
          }
          if (D < 10) {
            D = '0' + D;
          }
          const payDate =
            Y + '-' + M + '-' + D + ' ' + '10' + 
    // 今天点击事件
    toDay(index) {
        const date = new Date();
        this.num = index;
        this.$set(this.model, 'chargeTime', date);
      },
     // 昨天点击事件
      yesterDay(index) {
        const date = new Date();
        date.setTime(date.getTime() - 3600 * 1000 * 24);
        this.num = index;
        this.$set(this.model, 'chargeTime', date);  
      },
      // 前天点击事件
      beforeYesterday(index) {
        const date = new Date(
          new Date() - 24 * 60 * 60 * 1000 - 24 * 60 * 60 * 1000,
        );
        this.num = index;
        this.$set(this.model, 'chargeTime', date);
      },
  }
};
</script>

<style lang="scss" scope>
 /* 本身样式 */
  .CClass {
    color: black;
    background-color: #dcdfe6;
    border: 0px;
  }
  /* 动态类名点击样式 */
  .Cclass {
    color: white !important;
    background-color: #409eff !important;
  }
</style>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值