Comments (15)
found a solution for now:
I can register it by using the registerModule()
method on the store, inside a plugin that is loaded after the axios initialization.
plugins/init-store-modules.js
export default async ({store, app}) => {
await store.dispatch('initStoreModules', {store: store, axios: app.$axios})
}
store/index.js
import Vuex from 'vuex'
import userStore from './users'
const createStore = () => {
return new Vuex.Store({
state: {
},
mutations: {
},
// modules: {
// users: userStore()
// },
actions: {
async initStoreModules ({state}, {store, axios}) {
await store.registerModule('users', userStore(axios));
}
}
})
}
export default createStore
You could also register that inside the plugin but I like to have the module registration inside the store.
@christianmalek Maybe that should be added to the readme with an example or similar.
from vuex-rest-api.
@christianmalek I think you could provide your package as a nuxt module, but that I would not know how to register the stores in a sane way.
from vuex-rest-api.
@juandl But you don't pass any specific axios instance in your code example which was necessary for nuxt-auth @appinteractive, doesn't it? So how does this work?
from vuex-rest-api.
Yes. It works with Nuxt.
from vuex-rest-api.
@christianmalek sorry I was not ready with the descriotion. I have an issue in conbination with nuxt-auth as the axios instance can`t be passed. At lease I don't know how.
from vuex-rest-api.
Could you please provide a link to nuxt-auth?
from vuex-rest-api.
sure added it to the description, ment auth-module and not nuxt-auth...
https://github.com/nuxt-community/auth-module
from vuex-rest-api.
currently I am at the state of having something ugly like this:
import Vuex from 'vuex'
import userStore from './users'
const createStore = () => {
return new Vuex.Store({
state: {
},
mutations: {
},
// modules: {
// users: userStore(Vuex)
// },
actions: {
nuxtServerInit ({dispatch}, {app}) {
dispatch('init', {store: app.store, axios: app.$axios})
},
async init ({state}, {store, axios}) {
await store.registerModule('users', userStore(axios));
}
}
})
}
export default createStore
with that I still have to call that on the client to instantiate the module with the correct axios instance
await this.$store.dispatch('init', {store: this.$store, axios: this.$axios})
from vuex-rest-api.
Hey @appinteractive !
I've just tried 30min to make the axios instance accessible to vuex-rest-api in combination with nuxt-auth (which uses nuxt-axios) and didn't find any solution. I didn't think about registering the store with an action. Nice idea!
At the moment I also don't know how I could simplify this process. Do you have any ideas?
I will definitely add this to the readme. Thanks!
from vuex-rest-api.
I will look into this. But I don't have much experience with nuxt.
from vuex-rest-api.
@appinteractive I've added an section: https://christianmalek.github.io/vuex-rest-api/miscellaneous.html#nuxt
from vuex-rest-api.
Yeah thanks for mentioning 😀
from vuex-rest-api.
@christianmalek New Update:
store/index.js
import Vuex from 'vuex'
import news from "./posts";
const createStore = () => {
return new Vuex.Store({
...news
})
}
export default createStore
store/posts.js
import Vapi from "vuex-rest-api";
const posts = new Vapi({
baseURL: "http://demo.com/api",
state: {
posts: []
}
})
.get({
action: "getPosts",
property: "posts",
path: "/posts"
})
.getStore();
export default posts;
from vuex-rest-api.
Hello @juandl !
The issue was passing a specific axios instance to Vapi. Does this work with your code?
from vuex-rest-api.
Hello @christianmalek, I had the same problem in Huxt.js in this way I solved it.
from vuex-rest-api.
Related Issues (20)
- What does pending really mean? Not sure if this is intended behavior. HOT 2
- V3 - Checklist HOT 2
- Handling rejected promises HOT 3
- Update axios peer dependency HOT 6
- Can't install HOT 5
- Not possible to create a pr HOT 3
- [QUESTION] - Pre API Call Transformation HOT 3
- Caching requested data HOT 8
- Using webpack: vuex_rest_api__WEBPACK_IMPORTED_MODULE_0___default.a is not a constructor HOT 4
- Correct way to change state HOT 9
- provide action payload in onSuccess HOT 4
- Add support of mapping response object to typed model objects HOT 1
- Section "calling-the-actions" disapeared from the docs HOT 2
- Not an issue tip for CORS. [Tips] HOT 1
- Callback when rest call completes HOT 2
- Support for Fetch API HOT 4
- Add ability to add "namespaced" option for "getStore" HOT 4
- How to get rid of error&pending in setting a custom state? HOT 7
- Update peer dependency of axios HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vuex-rest-api.