How to pass a method in Vue.js slot scope

后端 未结 3 2323
灰色年华
灰色年华 2021-02-20 16:38

I\'m using a slot scope in vuejs. It\'s working great. I can pass anything I want into the slot like this:

    

         


        
3条回答
  •  你的背包
    2021-02-20 16:55

    Reading this article I have discovered that if you need to call a method programmatically (not from the template), you can actually pass a method from the parent component to the scoped-slot, but then you have to pass the same method to a child component through a prop: in this way you have access to the method, and can call it from the code.

    This is how I used it:

    Usage (in html, blade or other vue component)

    
    

    Parent component (CartValidation.vue)

    
    
    

    Child component (Cart.vue)

    
    

    In other parts of my code I used other elements inside the slot, but in each child component I could call this.myMethod(). I hope this helps other people :)

提交回复
热议问题