Right now we can access pinia store only with Vue app
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import mainStore from 'store/main.store';
const pinia = createPinia()
const app = createApp(App)
app.use(pinia)
const store = mainStore();
app.mount('#app')
But how we access Pinia store without Vue app? Somehting like this, as it was done with vuex.
import { createPinia } from 'pinia'
import mainStore from 'store/main.store';
const pinia = createPinia()
const store = mainStore();
Looking at the source code, pinia is implemented using the vue plugin, and the pinia object is passed with provide() inject(), so it cannot run without the vue environment.
Related
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')
I was trying to use vuex in my vue project.When I directly use the store everything is fine.But when I try to put the store in module then use it,everything just gone.
Also,if I import router from my router.js and use it ,everything will gone.
Super confused about it.
the way I directly use it
import a from "./store/modudel/a"
the way I put in module
// main.js
import store from "./store/index";
createApp(App)
.use(store)
.mount("#app");
// store/index.js
import Vue from 'vue';
import Vuex from 'vuex';
import a from './module/a';
Vue.use(Vuex);
export default new Vuex.Store({
modules: {
a
}
});
Your store/index.js contains code specific to Vue 2, but your main.js is for Vue 3. This code should be causing linter errors in the dev terminal or runtime errors in the browser console.
Assuming you have a Vue 3 project with Vuex 4, the equivalent store code would be:
// store/index.js
import { createStore } from 'vuex';
import a from './module/a';
export default createStore({
modules: {
a
}
});
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
Hi everyone I am using Aos libray in my Vue 3 project
import App from './App.vue'
import AOS from 'aos'
import 'aos/dist/aos.css'
import { createApp } from "vue";
const app = createApp({
render: (h) => h(App),
created(){
AOS.init()
}
});
app.mount('#app')
And add
<div data-aos="fade-up">#MY codes block<div>
But project not working What is the problem ?? Thanks ..
I had the same problem
My solution was:
import AOS from 'aos'
import 'aos/dist/aos.css'
createApp(App)
.use(AOS.init())
.mount("#app");
You should try this :
import App from './App.vue'
import AOS from 'aos'
import 'aos/dist/aos.css'
import { createApp } from "vue";
export const app = createApp(App)
app.AOS = new AOS.init({ disable: "phone" });
app.use(AOS).mount('#app')
I'm trying to use Vuex with Vue 3. Here is my code in main.js:
import { createApp } from 'vue';
import App from './App.vue';
import Vuex from 'vuex';
const app = createApp(App);
app.use(Vuex);
const store = new Vuex.Store({
state: {
counter: 0
}
});
app.store(store);
app.mount('#app');
When trying this, I get the following error:
Uncaught TypeError: Vue is not a constructor
I have tried using the syntax recommended in the official Vuex docs but this does not work either. Does anyone know how to fix this?
This error occurs because you are calling new Vuex.Store as is done for Vuex 3.
If you are using Vue 3, you must install Vuex 4 by issuing:
# npm
npm install --save vuex#next
# yarn
yarn add vuex#next
Then build your store and "inject" it to the Vue instance as follows:
import { createStore } from 'vuex';
import { createApp } from 'vue';
const store = createStore({
state () {
return {
count: 1
}
}
})
const app = createApp({ /* your root component */ });
app.use(store);
Refer to Vuex docs for more details:
Vuex 4 for Vue 3