点击未传递到Vue 2中的自定义组件

时间:2017-06-05 11:43:29

标签: javascript vuejs2 vue-component vuetify.js

我收到了很多这样的帖子:

<template>
  <div id="posts">
    <div id="posts-wrapper">
      <post v-for="(item, key) in posts" :key="item.id" :post="item" :isListItem="true"></post>
    </div>
    <infinite-loading :on-infinite="onInfinite" ref="infiniteLoading">
      <span slot="no-more">
        There are no more posts
      </span>
    </infinite-loading>
  </div>
</template>

<script>
import InfiniteLoading from 'vue-infinite-loading'
import posts from '@/api/posts'
import post from '@/components/Post'

export default {
  name: 'posts',
  components: {
    post,
    InfiniteLoading,
  },
  data () {
    return {
      posts: []
    }
  },
  methods: {
    onInfinite() {
        posts.getPosts(page++).then(posts => {
        this.posts = this.posts.concat(posts);
        this.$refs.infiniteLoading.$emit('$InfiniteLoading:loaded');
      })
    }
  }
};
</script>

这是post组件:

<template>
  <v-card class="post-list-item" v-bind:class="{ clickable: isListItem }" @click.native="openPost">
    <div class="header">
      {{ post.name }}
    </div>
    <div class="image">

    </div>
    <div class="data">

    </div>
    <!-- same handler bound here just for testing -->
    <div class="button" v-on:click.native="openPost">
      Open post
    </div>
  </v-card>
</template>

<script>
  export default {
    name: 'post',
    methods: {
      openPost() {
        console.log('post opening');
      }
    },
    props: {
      post: {
        type: Object,
        required: true
      },
      isListItem: {
        type: Boolean,
        default: false
      }
    },
    data () {
      return {}
    }
  }
</script>

问题是点击不会通过,即。永远不会调用openPost()方法。如果我在父母中设置一个监听器,那么这是有效的,但不是我正在寻找的。点击应该在组件本身内处理。

v-cardVuetify component

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,但我使用

解决了这个问题
v-on:click

而不是

@click.native