How to use DefineComponent() with Composition API in Vue, when components have the same methods? - vue.js

I am trying to use a plugin VueDraggableNext in a project that also used Vuetify. Both plugins have a <draggable> element.
If I use the 'Options' API with Vue, I can use the following method to define which version of <draggable> I want to use:
<script>
....
import { defineComponent } from 'vue'
import { VueDraggableNext } from 'vue-draggable-next'
export default defineComponent({
components: {
draggable: VueDraggableNext,
},
....
....
and it works. But I wish to use the Composition API (it's what I'm used to!) - what is the syntax for defineComponent() in this case? I have tried re-ordering the imports, but to no effect, the vuetify version of <draggable> is always used.
main.js:
import { createApp } from 'vue'
import App from './App.vue'
import { VueDraggableNext } from'vue-draggable-next'
// Vuetify
import 'vuetify/styles'
import { createVuetify } from 'vuetify'
import * as components from 'vuetify/components'
import * as directives from 'vuetify/directives'
const vuetify = createVuetify({
components,
directives,
})
import '../assets/main.css'
createApp(App).use(vuetify).use(VueDraggableNext).mount('#app')
and I have tried the following in my vue.js but get error 'components is not defined'.
<script setup>
import { reactive,components} from 'vue'
import { defineComponent } from 'vue'
import { VueDraggableNext } from'vue-draggable-next'
defineComponent(
components = {
draggable: VueDraggableNext }
)
....
....

Related

Does not provide an export named 'Vue' (at main.js?t=1667997788986:1:9)

I am developing an application in Express and Vue js
I'm getting this:
Uncaught SyntaxError: The requested module '/node_modules/.vite/deps/vue.js?v=460a75c2' does not provide an export named 'Vue' (at main.js?t=1667997788986:1:9)
App.vue file :
import {Vue, createApp } from 'vue'
import App from './App.vue'
import './assets/main.css'
import axios from 'axios'
import vueAxios from 'vue-axios'
Vue.use(axios, vueAxios)
createApp(App).mount('#app')
My main.js file is :
<script>
export default {
data(){
return{
dataResponse: [],
}
},
methods: {
async getDataResponse(){
const res = await axios.get(`${this.baseUrl}/catalog/home`)
console.log(response)
}
},
created(){
this.getDataResponse()
}
}
</script>
<template>
<h1>Welcome</h1>
</template>
You didn't write which version of Vue you are using, but based on the fact that you are using createApp it looks like you are using Vue 3.
In that case, the error is correct -- there is no export called Vue in the module vue. Vue 3 removed the default export, so you can't write import Vue from 'vue' anymore, and there is no export named Vue in the package, so import { Vue } from 'vue' is not valid either.
You're probably trying to follow an old tutorial about installing Vue plugins. In Vue 3, instead of writing Vue.use(plugin), you would write createApp().use(plugin) instead.
If your are using Vue 2.7 and 3.x, do you mind if try import { createApp } from 'vue', and then use createApp(App).use(axios).use(vueAxios).mount('#app')? It should be works because of import nature.
Another alternative:
import { createApp } from 'vue'
import App from './App.vue'
import './assets/main.css'
import axios from 'axios'
import vueAxios from 'vue-axios'
const app = createApp(App)
app.use(axios)
app.use(vueAxios)
app.mount('#app')

Can't access instance of createApp exported in vue 3

I have a instance of createApp and use this in vuetify plugin.
main file
import { createApp } from 'vue'
import App from './App.vue'
export const app = createApp(App)
import '#/plugins/vuetify.ts'
vuetify plugin
import '#mdi/font/css/materialdesignicons.css'
import 'vuetify/styles'
import { createVuetify } from 'vuetify'
import {app} from '#/main.ts'
app.use(createVuetify())
error: "Uncaught ReferenceError: Cannot access 'app' before initialization at vuetify.ts:7:1"
Thank you in advance.
import statements are hoisted, putting other code above them gives false impression that it will be evaluated first.
main.ts and vuetify.ts contain circular dependency that cannot be resolved. In order to avoid this, app instance should be created in a separate module, app.ts, then it can be imported in both.
Nothing in vuetify.ts requires to execute use() inside this module. A common way is to install components where app is defined, i.e. in main entry point:
vuetify.ts
import '#mdi/font/css/materialdesignicons.css'
import 'vuetify/styles'
export { createVuetify } from 'vuetify'
main.ts
import { createApp } from 'vue'
import App from './App.vue'
import { createVuetify } '#/plugins/vuetify.ts'
export const app = createApp(App).use(createVuetify())

vue2 and composition api - cannot import store, error [vue-composition-api] must call Vue.use(VueCompositionAPI) before using any function

I am using vue 2.6.14 and composition-api 1.3.3 package to use composition api. I have
my main.js like
import Vue from 'vue'
import VueCompositionAPI from '#vue/composition-api'
Vue.use(VueCompositionAPI)
import App from './App.vue'
import router from './router'
Vue.config.productionTip = false
new Vue({
router,
render: h => h(App)
}).$mount('#app')
I try to setup a store
I have a src folder / store folder / index.js
and inside the index.js
import { reactive } from '#vue/composition-api'
const state = reactive({
counter : 0
})
export default{ state }
inside App.vue I try to import store to use it
<script>
import store from '#/store'
</script>
I get the error Uncaught Error: [vue-composition-api] must call Vue.use(VueCompositionAPI) before using any function.
I tried all solutions from here and nothing works. If I remove the import store from '#/store' the error goes away. Using vue 3 s not an option.
How can I solve this?
Thanks
imports are automatically hoisted to the top of the file, so it actually precedes the Vue.use(VueCompositionApi) at runtime.
So these lines:
import Vue from 'vue'
import VueCompositionAPI from '#vue/composition-api'
Vue.use(VueCompositionAPI)
import App from './App.vue' 👈 hoisted
...become:
import Vue from 'vue'
import VueCompositionAPI from '#vue/composition-api'
import App from './App.vue' 👈
Vue.use(VueCompositionAPI)
So the plugin doesn't get installed before App.vue gets imported, leading to the error you observed.
Option 1: Move plugin installation to own file
You can move the installation of #vue/composition-api into its own file that could be imported before App.vue:
// lib/composition-api.js
import Vue from 'vue'
import VueCompositionAPI from '#vue/composition-api'
Vue.use(VueCompositionAPI)
// main.js
import Vue from 'vue'
import './lib/composition-api'
import App from 'App.vue'
demo 1
Option 2: Use require() in App.vue
require the store in the component's setup(), where the #vue/composition-api would've already been installed:
// App.vue
import { defineComponent, computed } from '#vue/composition-api'
export default defineComponent({
setup() {
const store = require('#/store').default
return {
counter: computed(() => store.state.counter),
increment: () => store.state.counter++,
}
},
})
demo 2
Option 3: Use import() in App.vue
Dynamically import the store with import(). This is especially needed in Vite, which does not have require().
// App.vue
import { defineComponent, computed, ref } from '#vue/composition-api'
export default defineComponent({
setup() {
const store = ref()
import('#/store').then(mod => store.value = mod.default)
return {
counter: computed(() => store.value?.state.counter),
increment: () => store.value && store.value.state.counter++,
}
},
})
demo 3

Add only Vuetify data table component to existing Vuejs Project

I need to add Vuetify Data Table to my existing Vuejs project. So when we add vue add vuetify its installed globally. Please help me for that how doing it
Thanks
You can import the dataTable component alone.
You can do this in a vuetify.js file:
// src/plugins/vuetify.js
import Vue from 'vue'
import Vuetify, {
VDataTable,
} from 'vuetify/lib'
Vue.use(Vuetify, {
components: {
VDataTable,
},
})
const opts = {}
export default new Vuetify(opts)
and in your main.js import this file: import vuetify from #/src/plugins/vuetify.js
or you can import it in the component you need it:
<!-- Vue Component -->
<template>
<v-card>
<v-card-title>...</v-card-title>
<v-card-text>...</v-card-text>
</v-card>
</template>
<script>
import { VDataTable } from 'vuetify/lib'
export default {
components: {
VDataTable,
}
}
</script>
you can read more about this in vuetify docs

How to configure correctly vuelidate?

Using the latest version of Vue
I've been trying to make Vuelidate work
Every guide I see on the internet using this type of "Main.js" code:
import Vue from 'vue';
import Vuelidate from 'vuelidate';
import App from './app/App';
Vue.use(Vuelidate);
new Vue({
el: '#app',
render: h => h(App)
});
While my main.js looks like this:
import { createApp } from 'vue'
import App from './App.vue'
createApp(App).mount('#app')
Trying to use in my component as followed:
import { required, email } from '#vuelidate/validators'
import {useVuelidate} from "#vuelidate/core";
export default {
data: () => ({
v$: useVuelidate(),
Trying to implement it like this:
<div v-if="submitted && !$v.validations.affiliate.required"></div>
Getting this error:
unable to resolve type of v$
import { createApp } from 'vue'
import App from './App.vue'
import Vuelidate from 'vuelidate'
createApp(App).use(Vuelidate).mount('#app')