2

How do I import globally with Vue 3 in the main.js the ref, reactive and computed?

I'm trying to avoid doing this in each component:

import { ref, reactive, computed } from 'vue'
Penny Liu
  • 11,885
  • 5
  • 66
  • 81
Adri HM
  • 1,183
  • 2
  • 12
  • 21

2 Answers2

3

Not sure this is a good idea (it likely defeats tree shaking), but it's possible to make them global by adding them to window:

// main.js
import { ref, reactive, computed } from 'vue'

window.ref = ref
window.reactive = reactive
window.computed = computed

If using ESLint, make sure to configure these globals:

// eslintrc.js
module.exports = {
  globals: {
    ref: true,
    reactive: true,
    computed: true,
  }
}
tony19
  • 99,316
  • 15
  • 147
  • 208
1
npm i vue-global-api
// main.js
import 'vue-global-api'

https://www.npmjs.com/package/vue-global-api

richardec
  • 14,202
  • 6
  • 23
  • 49
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 08 '22 at 11:47