亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

vue如何根據配置監聽的事件類型?

vue如何根據配置監聽的事件類型?

白豬掌柜的 2018-11-24 14:18:32
Vue.component('button-counter', {  template: '<button v-on:click="increment()">{{ counter }}</button>',  data: function () {    return {      counter: 0    }  },  methods: {    increment: function () {      this.counter += 1    }  },})比如上面的組件,我希望 v-on監聽的事件是 父組件傳遞過來的,而不是在這里寫死為click,我該怎么寫?我當然知道使用props傳遞,我想知道v-on后面該怎么寫。如果直接寫propname的話vue會認為要監聽的事件是propname,而不是具體的事件。
查看完整描述

1 回答

?
RISEBY

TA貢獻1856條經驗 獲得超5個贊

題主的需求比較特殊,如果是這樣的話可能只能使用render代替template了:

<div id="app">

  <button-counter :event="'click'">abc</button-counter>

</div>

const counter = Vue.component('button-counter', {

  render(createElement) {

    return createElement(

      'button', {

        on: {

          [this.event]: this.increment,

        },

      },

      `${this.counter}`,

    )

  },

  data() {

    return {

      counter: 0,

    }

  },

  props: {

    event: String

  },

  methods: {

    increment() {

      this.counter += 1

    },

  },

})


new Vue({

  el: '#app',

  components: {

    'button-counter': counter,

  },

})


查看完整回答
反對 回復 2018-12-18
  • 1 回答
  • 0 關注
  • 484 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號