Vue: Scoped slots: data from `slot-scope` can't be passed to custom directives

Created on 11 May 2018  Â·  5Comments  Â·  Source: vuejs/vue

Version

2.5.16

Reproduction link

https://jsfiddle.net/4begnsym/5/

Steps to reproduce

  • Open the link
  • Open the browser dev tools to see the JavaScript error

What is expected?

I would expect that I can use data from the slot-scope as parameters in custom directives.

What is actually happening?

Data from slot-scope is undefined in the custom directive.

Most helpful comment

Please reopen or explain further why I have access to someMethod in @click but not in the directive?

I'm not sure if I got your point clearly, you actually can access to someMethod in the directive, according to my tests, to invoke the function someMethod directly, you can just do binding.value() - https://jsfiddle.net/4begnsym/7/.

All 5 comments

child-cmp was rendered under the context of the root instance, rather than parent-cmp, in your case, you can use vnode.parent.componentInstance to refer to the parent component, like https://jsfiddle.net/4begnsym/6/

@javoski thank you for your response, but did you see that I use slot-scope?

Please reopen or explain further why I have access to someMethod in @click but not in the directive?

In my opinion, I should have access to someMethod passed into the component via the slot-scope.

Please reopen or explain further why I have access to someMethod in @click but not in the directive?

I'm not sure if I got your point clearly, you actually can access to someMethod in the directive, according to my tests, to invoke the function someMethod directly, you can just do binding.value() - https://jsfiddle.net/4begnsym/7/.

@maoberlehner your way can't work because the someMethod method is defined in the parent-comp, but v-node.context is the main Vue instance - because the directive was evaluated in the scope of the main app's template.

So everything's correct here.

@javoski @LinusBorg Oh my... I'm sorry. Idk where I copy pasted the custom directive snippet – my fault to not look up how to access the value the correct way.

I apologize for the trouble.

Was this page helpful?
0 / 5 - 0 ratings