在 Vue.js 中將事件和參數傳遞給 v-on - Passing event and argument to v-on in Vue.js

問題:

I pass a parameter in v-on:input directives.我在v-on:input指令中傳遞了一個參數。 If I don't pass it, I can access the event in the method.如果我不通過它,我可以在方法中訪問該事件。 Is there any way I can still access the event when passing the parameter to the function.有什麼方法可以在將參數傳遞給函數時仍然訪問事件。 Not that I am using vue-router:並不是說我在使用 vue-router:

This is without passing the parameter.這是沒有傳遞參數。 I can access the event object我可以訪問事件對象

HTML HTML

<input type="number" v-on:input="addToCart" min="0" placeholder="0">

Javascript Javascript

  methods: {
    addToCart: function (event) {

      // I need to access the element by using event.target
      console.log('In addToCart')
      console.log(event.target)
    }
  }

This is when passing the parameter.這是在傳遞參數時。 I can't access the event object我無法訪問事件對象

HTML HTML

<input type="number" v-on:input="addToCart(ticket.id)" min="0" placeholder="0">

Javascript Javascript

  methods: {
    addToCart: function (id) {

      // How can I access the element by using event
      console.log('In addToCart')
      console.log(id)
    }
  }

Here is some snippet of the code, should be good enough to replicate the problem that I am having這是代碼的一些片段,應該足以複製我遇到的問題

https://jsfiddle.net/lookman/vdhwkrmq/ https://jsfiddle.net/lookman/vdhwkrmq/


解決方案:

參考一: https://en.stackoom.com/question/2lqhT
參考二: https://stackoom.com/question/2lqhT
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章