账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    vue在v-for,循环中,用定时器处理一个参数,第一次能够接收到数据,当定时器跑起来就为空了
    30
    0

    先列出数据格式

     [{username: "1", content: "2", dataTime: 1534128070979},{username: "11", content: "22", dataTime: 1534128070979}]

    从vuex中获取到的数据,然后循环渲染。dataTime是一个时间戳,然后处理的方法是_updateTimeString。再将这个方法用定时器处理。我发现只能接收到第一次的数据item的数据,其他的为空了。一开始以为是的前后顺序的问题,各种尝试没能结解决。现在我怀疑的方向是双括号模板中可能就渲染一次了,就会造成不再往下穿参了。也考虑在{{}}中直接返回一个变量,但是因为循环中在处理就比较麻烦了,求指点迷津

    <template>
      <div class="comment-list" v-if="comments().length>0">
        <div class='comment' v-for="(item,index) in comments()" :key="index">
            <div class='comment-user'>
              <span class='comment-username'>
              {{item.username}}:
              </span>
            </div>
            <p>{{item.content}}</p>
            <span class='comment-createdtime'>
             {{_updateTimeString(item.dataTime)}}
            </span>
            <span class='comment-delete' @click="handleDeleteComment(index)">
              删除
            </span>
          </div>
      </div>
    </template>
    
    <script>
    
    import { mapState, mapMutations, mapGetters } from "vuex";
    
    export default {
      name: 'comment-list',
      data () {
        return {
          timer : Object,
          timeString:"123"
        }
      },
      created(){
        // this.timer = setInterval(this._updateTimeString,1000)
      },
      methods:{
        ...mapState({
          comments:state=>state.comments
        }),
        handleDeleteComment(index){
          console.log(this.comments())
          let newComment = this.comments().splice(index,1);
          localStorage.setItem("comments",JSON.stringify(this.comments()))
        },
        _updateTimeString:(item)=>{
          let duration = (+new Date() - item) / 1000;
          let timeString = duration > 60?`${Math.round(duration/60)}分钟前`:`${Math.round(duration)}秒前`;
          return timeString;
       }    
      },
      computed:{
       //  _updateTimeString:(item)=>{
       //  let thisItem = item;
       //  let duration = (+new Date() - item) / 1000;
       //  let timeString = duration > 60?`${Math.round(duration/60)}分钟前`:`${Math.round(duration)}秒前`;
       //  console.log(thisItem)
       //  return timeString;
       // }
      
      },
      mounted(){
        // this.timer = setInterval(this._updateTimeString,1000)
      }
    }
    </script>
    
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 随性洒脱 普通会员 1楼

      在 Vue 中,当你使用 v-for 或者循环中的 v-for 时,你需要将参数绑定到数组的下标。例如,如果你有一个数组 items,并且你想在每条数据上添加一个定时器,你可以这样做:

      ```html

      ```

      然后在 handleItem 方法中,你可以使用定时器来处理参数:

      js methods: { handleItem() { // 使用定时器处理参数 this.$set(this, 'item', item); } }

      当定时器被触发时,它会更新数组的 item 数组,并将这个值传递给 handleItem 方法。在 handleItem 方法中,你可以使用这个值来处理数组中的数据。

      注意,这种方法的缺点是它会直接修改数组的 item 数组,而不是在循环内部处理。如果你想在循环内部处理数组中的数据,你可以使用其他方法,例如使用数组的 map 方法或者 filter 方法。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部