Javascript v-if在v-for内显示一个截面

Javascript v-if在v-for内显示一个截面,javascript,vue.js,vuejs2,Javascript,Vue.js,Vuejs2,我试着做一些类似linkedin评论的事情,虽然页面上显示了多个帖子,但如果你点击该帖子的“显示评论”,则只会显示该帖子的评论 我的解决方案是:在created()中生成162个布尔变量,然后使用v-for的索引,我会写我的v-if,但它不会打开我想要的div。在created()中必须提到,当我将变量设为true时,它们都会打开。我们只显示162个帖子 当它为false时,单击它们的值后会变为true(使用console.log检查),但div不会打开 <template> &

我试着做一些类似linkedin评论的事情,虽然页面上显示了多个帖子,但如果你点击该帖子的“显示评论”,则只会显示该帖子的评论

我的解决方案是:在created()中生成162个布尔变量,然后使用v-for的索引,我会写我的v-if,但它不会打开我想要的div。在created()中必须提到,当我将变量设为true时,它们都会打开。我们只显示162个帖子

当它为false时,单击它们的值后会变为true(使用console.log检查),但div不会打开

<template>
  <span v-if="post.comments_status == 1" class="optiontext cursor-pointer" @click="showcomment(index)"><i
            class="fa fa-comment-o"></i> OPEN COMMENT SECTION FOR THIS POST 
  </span>

<div class="col-md-8" id="imtop" style="direction:rtl">

    <!-- v-if part to show comments-->
    <div v-if="Commenton[index] == true" class="col-md-12 commentsec">
        <div class="eachcomment col-md-12">
            <div class="usercomment">
                <img :src="images" class="pop-img-min"/>
            </div><!-- usercomment end-->
        </div>

    </div><!-- end of allposts-->
</div>
</template>

<script>
  import vSelect from 'vue-select'
  import axios from 'axios'
  export default {

  components: {vSelect},
  props: ['alltags','posts'],
  data() {
    return {
      searchoptions:[{label:'جدیدترین ',value:'newest'},{label:'محبوبترین',value:'محبوبترین'}],
      Commenton:[],
    }
  },

  created() {
    for(var i = 0; i<162;i++) {
      this.Commenton[i] = false;
    }

  },
  mounted() {
    this.getInitialUsers();
    this.scroll(this.post);
  },
  methods: {
    showcomment(indexof){
      if(this.Commenton[indexof] == false){
        this.Commenton[indexof]=true;
      }else if(this.Commenton == true) {
        this.Commenton=false;
      }
    },

  },

}


</script>

打开此帖子的评论部分
从“vue select”导入vSelect
从“axios”导入axios
导出默认值{
组件:{vSelect},
道具:['alltags','posts',],
数据(){
返回{
搜索选项:[标签:最新],[标签:最新],[价值:最新],[标签:最新],[价值:最新],
评论:[],
}
},
创建(){
对于(var i=0;i

这些问题是由以下事实造成的:由于Javascript环境中的限制,vue无法检测数组修改。要确保vue看到您的更改,请使用
this.$set(对象、键、值)

这些问题是由以下事实造成的:由于Javascript环境中的限制,vue无法检测数组修改。要确保vue看到您的更改,请使用
this.$set(对象、键、值)


一个简单的问题!有没有比制作162数组更好的方法?一个场景也足够了。谢谢你的清晰回答。顺便问一下!制作一个名为“selectedPostId”的变量怎么样然后检查循环时的
comment.postid===selectedPostId | | selectedPostId===undefined
,这就是我编写一个快速问题的方式!有没有比生成162数组更好的方法?一个场景也足够了。谢谢你的清晰回答,顺便说一句!生成一个名为“selectedPostId”的变量怎么样然后在循环时检查
comment.postid===selectedPostId | | selectedPostId===undefined
,我就是这样编码的
// Inside your created method:
this.$set(this.Commenton, i, false);

// inside you showComment method
    showcomment(indexof){

        if(this.Commenton[indexof] == false){
            this.$set(this.Commenton, indexof, true);
        }else if(this.Commenton[indexof] == true) {
            this.$set(this.Commenton, indexof, false);
        }


   },