Skip to content

[QUESTION ] Does exist clean solution for passing string literal attribute into my custom directive without having error? #7654

@jnahlovsky

Description

@jnahlovsky

What problem does this feature solve?

Hey guys,
I'm just trying to write very simple custom directive, but I've got a problem with console error,
which told me this:

[Vue warn]: Property or method "testModal" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option or for class-based components, by initializing the property. See https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

What I'm trying to solve is to make a directive as a trigger for the modal component (action mutating vuex store state). This simple directive has only bind and unbind hooks and looks like this example.

HTML

<button v-show-modal="testModal">Click here</button>

JS

{
  bind (el, binding) {
    el.event = event => {
      Store.dispatch(MODAL_ACTIONS.SHOW_MODAL, binding.expression)
    }
    el.addEventListener('click', el.event)
  },
  unbind (el) {
    el.removeEventListener('click', el.event)
  }
}

Problem disappears when I use single quotes inside double quotes
v-show-modal="'testModal'"

What does the proposed API look like?

More convenient would be adding param to directive object itself (I found this online, but it doesn't work for me, probably it's from older version of Vue)

{
  isLiteral: true,
  bind: //....
}

or something like this (same case, found it, but seems to not working anymore)

<button v-show-modal.literal="testModal">Click here</button>

Anyway, I jumped into docs and guide and didn't find any suggestion about this, so maybe there is a solution, but I just didn't come across it. Thanks in advance for any help :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions