Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom directives parse Error #1732

Closed
yangliguo7 opened this issue Nov 30, 2021 · 6 comments
Closed

custom directives parse Error #1732

yangliguo7 opened this issue Nov 30, 2021 · 6 comments
Labels
needs info needs repro Need a repository that can reproduce the problem, or a link to DEMO.

Comments

@yangliguo7
Copy link

if I have a custom directives like

const app = createApp(App);
app.directive("operate", {
    mounted(el, binding) {
        const { arg, instance, value } = binding;
        console.log(binding);
        const { param } = value || {};
        if (!instance?.[arg]) {
            console.log('not exist ')
            return;
        }
        el.addEventListener('click', instance[arg].bind(instance, param));
    },
})

in my program

<template>
  <button  v-operate:add >button</button>
</template>

<script setup>
const add = () =>{
  console.log('click trigger')
  alert('click trigger')
}
</script>

'add ' is assigned a value but never used no-unused-vars

Should I export this method separately to avoid eslint errors?

like

<script>
export const add = () =>{
  console.log('click trigger')
  alert('click trigger')
}
</script>

Is there another way to avoid this mistake ?

my eslintrc.js

module.exports = {
  root: true,
  env: {
    node: true,
  },
  extends: ['@vue/airbnb', '@vue/prettier', 'plugin:vue/base'],
  parserOptions: {
    parser: 'babel-eslint',
  },
  overrides: [
    {
      files: ['**/__tests__/*.{j,t}s?(x)', '**/tests/unit/**/*.spec.{j,t}s?(x)'],
      env: {
        jest: true,
      },
    },
  ],
  globals: {
    defineEmits: true,
    defineProps: true,
    defineExpose: true,
  },
};
@ota-meshi
Copy link
Member

Thank you for posting the issue.
We probably need to add an option to the script-setup-uses-vars rule.

https://eslint.vuejs.org/rules/script-setup-uses-vars.html

@ota-meshi
Copy link
Member

I've tried your source code but it doesn't work in Vue.
Can you provide a sample repository where your custom directives with <script setup> work well?

@ota-meshi ota-meshi added needs info needs repro Need a repository that can reproduce the problem, or a link to DEMO. and removed enhancement labels Feb 8, 2022
@ota-meshi
Copy link
Member

It doesn't seem to work now due to a bug, but it seems that you essentially need to use defineExpose.
vuejs/core#5018 (comment)

I close this issue as there are no false positives when using defineExpose.

@yangliguo7
Copy link
Author

I do know that using defineExpose works in dev , but sometimes I don't want to use it to expose it。 @ota-meshi

@ota-meshi
Copy link
Member

I don't know how to get your directive to work without using defineExpose. Please let me know if you know it.

@yangliguo7
Copy link
Author

yangliguo7 commented Feb 28, 2022

hi~~
in vue@3.0.0,If I use setup, I get my directive to work without using defineExpose

<template>
  <button @click="clickA">A</button>
  <button v-operate:clickB>B</button>
</template>
<script setup>
const clickA =  () =>{
  alert("1")
}
const clickB =  () =>{
  alert("2")
}

</script>

@ota-meshi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs info needs repro Need a repository that can reproduce the problem, or a link to DEMO.
Projects
None yet
Development

No branches or pull requests

2 participants