2017-02-07 16 views
6

Tôi đang cố gắng để thêm một handler tùy chỉnh InlineButtonClickHandler sự kiện click một thành phần <router-link>, vì vậy mà tôi có thể phát ra một tùy chỉnh appSidebarInlineButtonClick sự kiện.Thêm sự kiện nghe để <router-link> thành phần sử dụng "v-on:" Chỉ thị - VueJS

Nhưng mã của tôi không hoạt động. Tôi đang làm gì sai?

<template> 
    <router-link :to="to" @click="InlineButtonClickHandler"> 
    {{ name }} 
    </router-link> 
</template> 

<script type="text/babel"> 
export default { 
    props: { 
    to: { type: Object, required: true }, 
    name: { type: String, required: true } 
    }, 
    methods: { 
    InlineButtonClickHandler(event) { 
     this.$emit('appSidebarInlineButtonClick'); 
    } 
    } 
} 
</script> 
+0

bạn có thể hiển thị nhật ký bảng điều khiển không? – samayo

Trả lời

13

Bạn cần phải thêm .native modifier:

<router-link 
    :to="to" 
    @click.native="InlineButtonClickHandler" 
> 
    {{name}} 
</router-link> 

này sẽ lắng nghe sự kiện nhấp chuột mẹ đẻ của phần tử gốc của router-link thành phần.

+0

làm việc cho tôi ... Cảm ơn –

0

một giải pháp tốt đẹp sẽ là:

<a v-link='{name: "home"}' v-on:click.capture='InlineButtonClickHandler'>Home</a>

+0

Cảm ơn bạn đã trả lời. Nhưng theo tài liệu, giải pháp này đã được thay thế trong VueJs V2: [liên kết tới nguồn doc ...] (https://vuejs.org/v2/guide/migration-vue-router.html#v-link-replaced) –

0
<router-link:to="to"> 
    <span @click="InlineButtonClickHandler">{{name}}</span> 
</router-link> 

Có thể bạn có thể thử điều này.

Các vấn đề liên quan