2

I find it very repetitive to have to import vue things like: import { ref, computed } from 'vue' In the script setup section.

Would it be a bad practice to, let's say assign vue to a special character, like $ and then use it to access these like let drawer = $.ref(null);

If so what would be the reasoning behind?

1
  • If you are using typescript, it will be a problem because it lacks typing Commented Dec 5, 2022 at 2:54

2 Answers 2

1

You can use the experimental version of vue3:

// vite.config.js
export default {
  plugins: [
    vue({
      reactivityTransform: true
    })
  ]
}

after that there is an auto import available and you don't have to write .value if using a $ref or $computed.

Because $ref() is a macro and not a runtime API, it doesn't need to be imported from vue.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. This is a solution, and I will take a look. However, I'm more curious about the reasoning. ....
After looking deeper into the experimental version, I can say that it's focused on accessing variables with .value. The import becomes unnecessary as it is implemented with a macro.
Just a little update: Reactivity Transform was an experimental feature, and has been removed in the latest 3.4 release. More info here.
1

You can use unplugin-auto-import: https://github.com/antfu/unplugin-auto-import

Example using vite.config.js:

import AutoImport from 'unplugin-auto-import/vite'

export default defineConfig({
    plugins: [AutoImport({imports: ['vue']})]
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.