首页 > 解决方案 > 点击详细信息上的Vue列表项

问题描述

我有这个 Vue 应用程序

var app = new Vue({
  el: '#main',
  delimiters: ['${', '}'],
  data () {
      posts: [
          {
            id: 1,
            title: 'Post title 1',
            description: 'Post description 1'
          },
          {
            id: 2,
            title: 'Post title 2',
            description: 'Post description 2'
          },
          {
            id: 3,
            title: 'Post title 3',
            description: 'Post description 3'
          }
      ],
  },
  methods: {
      getPostData: function (event) {

      }
  }
});

 <div id="main">
  <ul>
    <li v-for="(post, index) in posts"><a @click="getPostData">${ post.title }</a></li>
  </ul>
</div>
这里是点击项目的描述

我想单击列表中的一个项目并在#item-description div 中显示该项目的描述。

我如何对此 getPostData 进行编程以从我单击的项目中获取描述。

肿瘤坏死因子

标签: javascriptvue.jsvuejs2

解决方案


<div id="main">
  <ul>
     <li v-for="(post, index) in posts"><a @click="getPostData(post.description)">${ post.title }</a></li>
  </ul>
</div>

 methods: {
   getPostData: function (postDesc) {
     // you got the post Desc
   }
 }

推荐阅读