Import another store in the store root? #969
-
https://pinia.vuejs.org/cookbook/composing-stores.html#nested-stores Below works but has duplicationAs the example below shows I need to declare import { defineStore } from 'pinia';
import { useActionStore } from './actions'
export const myStore= defineStore('example', {
state: () => ({
insert: null,
rename: 123,
add: 'ok',
}),
actions: {
insert() {
const actionStore= useActionStore();
actionStore.myAction(this.insert);
},
add() {
const actionStore= useActionStore();
actionStore.myAction(this.add);
},
rename() {
const actionStore= useActionStore();
actionStore.myAction(this.rename);
},
},
}) Below is kind of what I wish forHere I declare it in the global scope of the file. It however throws an error that I don't have Pinia set up. import { defineStore } from 'pinia';
import { useActionStore } from './actions'
const actionStore= useActionStore();
export const myStore= defineStore('example', {
state: () => ({
insert: null,
rename: 123,
add: 'ok',
}),
actions: {
insert() {
actionStore.myAction(this.insert);
},
add() {
actionStore.myAction(this.add);
},
rename() {
actionStore.myAction(this.rename);
},
},
}) Is there a way I can do it in the global space like the second example? That to avoid duplicate and longer code. Any ideas are welcome. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 12 replies
-
Use plugin to add a method to every stores: function myPlugin() {
return {
callActionStoreMyMethodWith(...args) {
const actionStore = useActionStore()
return actionStore.myMethond(...args)
}
}
} // ...
insert() {
this.callActionStoreMyMethodWith(...)
} (or just extract that as a helper function.) |
Beta Was this translation helpful? Give feedback.
-
I saw the answer from @iendeavor but I'm not sure I like it. Would it be terrible to use the method directly without adding it as a const first? I've changed my example below where actionStore now is a method that can be used inside the store. import { defineStore } from 'pinia';
import { useActionStore as actionStore } from './actions'
export const myStore= defineStore('example', {
state: () => ({
insert: null,
rename: 123,
add: 'ok',
}),
actions: {
insert() {
actionStore().myAction(this.insert);
},
add() {
actionStore().myAction(this.add);
},
rename() {
actionStore().myAction(this.rename);
},
},
}) If this is bad practice or bad in any way, please let me know. |
Beta Was this translation helpful? Give feedback.
-
A more convenient way is to use a setup store (it's the third example at https://pinia.vuejs.org/introduction.html#basic-example but it's not covered much in docs yet) You can call |
Beta Was this translation helpful? Give feedback.
-
If you are using Pinia ouf of Vue/Pinia context do not cal import { setActivePinia, createPinia } from 'pinia';
import { createApp } from 'vue';
import { useSStore } from '@/modules/s';
import { useEnvironmentHandlerStore } from '@/common/stores/environment-handler';
import { useENVStore } from '@/common/stores/env';
// **this is wrong !!!**
const { getCompanyNameNormalized } = useSStore();
const environmentHandlerStore = useEnvironmentHandlerStore();
const ENVStore = useENVStore();
const getS3LogoUrl = () => {
const { url } = ENVStore.data.s3;
const cn: string = getCompanyNameNormalized().toLowerCase();
const dc = environmentHandlerStore.getDc();
const companyName = `${cn}${dc ? `_${dc}` : ''}`;
const containerName = `${companyName}_logo-images`;
return `${url}/${companyName}/${containerName}/terminal_logo.png`;
};
const getLogoUrl = () => {
const companyName: string = getCompanyNameNormalized().toLowerCase();
const url: string = 'https://cdn.com/public/';
return `${url}${companyName}/media/logo.png`;
};
export default {
getS3LogoUrl,
getLogoUrl,
}; |
Beta Was this translation helpful? Give feedback.
-
@posva hi there, i just stumbled upon this issue and was wondering is there by now any recommended way to also do this with an option store? we're currently not planning on using a setup store, so i am currently looking for the best way to do this within an option store.. |
Beta Was this translation helpful? Give feedback.
-
I like options API more, so it will be better to have a solution for options API. There may be many guys like me. So, thx very much @posva. you are Vue man, true hero. |
Beta Was this translation helpful? Give feedback.
A more convenient way is to use a setup store (it's the third example at https://pinia.vuejs.org/introduction.html#basic-example but it's not covered much in docs yet) You can call
useActionStore()
once in that scenario, outside of actions