在 v-for 中设置一个 v-on:click 指令

Set up a v-on:click directive inside v-for

我已经显示了包含一些信息的图像列表。我希望这些图像可以点击。单击时,它应该显示 div 并显示“HI!!”。我一直在尝试在 Vue 数据中添加一个变量作为 show:true,并尝试构建一些逻辑,在单击时显示变为 false。但是我一直没能实现。

下面是示例代码:

template>
    <div>
        <h1>SpaceX</h1>
        <div v-for="launch in launches" :key="launch.id" class="list" @click="iclickthis(launch)">
            <div ><img :src="launch.links.patch.small" alt="No Image" title={{launch.name}} /></div>
            <div>ROCKET NAME: {{launch.name}} </div>
            <div>DATE: {{ launch.date_utc}} </div>
            <div>SUCCESS: {{ launch.success}} </div>
            <div>COMPLETENESS: {{ launch.landing_success}} </div>
            
        </div>
        <!-- <v-model :start="openModel" @close="closeModel" /> -->
    </div>
</template>
<script>

import axios from 'axios'
export default {
  name: 'SpaceXTimeline',
  components: {
  },
  data: () => ({
    launches : [],
    openModel : false,
    show : true,
  }),
  methods: {
    iclickthis(launch) {
        // this should load a model search v-model / bootstrap on vue  npm install v-model 
        console.log(launch.name + " is launched");
        console.log("DETAILS: "+ launch.details);
        console.log("ROCKET INFO: "+ launch.links.wikipedia);
        console.log("CREW DETAILS: "+ launch.crew);
        console.log("Launchpad Name: "+ launch.launchpad);
        this.openModel = true;
    },
    closeModel () {
        this.openModel = false;
    }
  },
  async created() {
    const {data} = await axios.get('https://api.spacexdata.com/v4/launches');
    data.forEach(launch => {
      this.launches.push(launch);
    });
  }
}; 
</script>

<style scoped>
.list {
  border: 1px solid black;
}
</style>

谢谢,非常感谢。

v-model 是一个绑定而不是一个元素,除非你已经命名了一个组件?是“modal”的拼写错误吗?

不管怎样,听起来你想要一个 v-if:

<v-model v-if="openModel" @close="closeModel" />

示例:

new Vue({
  el: '#app',
  components: {},
  data: () => ({
    launches: [],
    openModel: false,
    show: true,
  }),
  methods: {
    iclickthis(launch) {
      // this should load a model search v-model / bootstrap on vue  npm install v-model
      console.log(launch.name + ' is launched');
      console.log('DETAILS: ' + launch.details);
      console.log('ROCKET INFO: ' + launch.links.wikipedia);
      console.log('CREW DETAILS: ' + launch.crew);
      console.log('Launchpad Name: ' + launch.launchpad);
      this.openModel = true;
    },
    closeModel() {
      this.openModel = false;
    },
  },
  async created() {
    const {
      data
    } = await axios.get('https://api.spacexdata.com/v4/launches');
    data.forEach(launch => {
      this.launches.push(launch);
    });
  },
})

Vue.config.productionTip = false;
Vue.config.devtools = false;
.modal {
  cursor: pointer;
  display: flex;
  justify-content: center;
  position: fixed;
  top: 0;
  width: 100%;
  height: 100vh;
  padding: 20px 0;
  background: rgba(255, 255, 255, 0.5);
}

img {
  cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

<div id="app">
  <h1>SpaceX</h1>
  <div v-for="launch in launches" :key="launch.id" class="list" @click="iclickthis(launch)">
    <div>
      <img :src="launch.links.patch.small" alt="No Image" :title="launch.name" />
    </div>
    <div>ROCKET NAME: {{ launch.name }}</div>
    <div>DATE: {{ launch.date_utc }}</div>
    <div>SUCCESS: {{ launch.success }}</div>
    <div>COMPLETENESS: {{ launch.landing_success }}</div>
  </div>
  <div v-if="openModel" @click="closeModel" class="modal">
    MODAL
  </div>
</div>