-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Global store discussion #13
Comments
It may be too early to declare victory, but I appear to have gotten a nice TypeScript workflow going using:
The root store class looks like this: export class RootStore {
public $add (name: string, store: any): void {
if (name in this) {
throw new Error(`Unable to add local store "${name}" as property already exists`)
}
this[name] = store
}
public $remove (name: string): void {
if ((name in this) && !name.startsWith('$')) {
delete this[name]
}
}
constructor (modules: Record<string, any> = {}) {
Object.keys(modules).forEach(key => {
this.$add(key, modules[key])
})
}
} Install looks like this: export function install (Vue: VueConstructor, modules: Record<string, any> = {}) {
Vue.prototype.$store = new RootStore(modules)
} My project's import Vue from 'vue'
import { RootStore, install } from '@/packages/store'
// local modules
import app, { AppStore } from './app'
import ui, { UiStore } from './ui'
// tell typescript about modules
declare module '@/packages/store/root' {
interface RootStore {
app: AppStore,
ui: UiStore,
}
}
// tell vue about the built-in store
declare module 'vue/types/vue' {
interface Vue {
$store: RootStore
}
}
// install and add modules
Vue.use(install, {
app,
ui,
})
// ensure other modules importing store directly know what it is
export default Vue.prototype.$store as RootStore You can add modules individually using a combination of instantiation and augmentation: // import root store
import store from '@/store'
// augment root store with the new property
declare module '@/packages/store/root' {
interface RootStore {
tools: ToolsStore,
}
}
// local vue class store setup
@VueStore
class ToolsStore {
resize () {
bus.call('tools/windows/resize')
}
}
// physically add the local store to the root store
store.$add('tools', new ToolsStore()) Autocomplete works great in I know some of the TS boilerplate looks a little funky, but:
Happy to hear any thoughts! |
@davestewart -- Took me a bit as I'm not as familiar with TypeScript, but I think I get the gist. Looks nice. Would this replace the entire provide/inject usage of the current release? setup () {
const { tools } = useStore()
return { tool }
// vs.
return { tools: this.$store.tools }
} Globally attaching the store would be great and eliminate some boilerplate at the component level. Also, just a note, Vue is no longer exposed in the vue3 package. It would instead have to be attached to the app instance...something like: app.config.globalProperties.$store = new RootStore(modules) // or something |
Hmmm. We have inverse knowledge-sets! I can only really comment regarding Vue 2 right now, but I'm finding that Vue 3 wise, I don't suppose it makes any difference, but you would have more choice. Seeing as Vue 3's setup function does make it easier to type variables, perhaps it's not even needed in Vue 3? |
I agree with Though, I have found a nice workflow with the current vue-class-store partnered with a jsconfig and jsdoc, which allows for typing "guides" but not necessarily strict type checking like what TS offers. I think the way you posted works really well and keeps in tune with how a lot of things are packaged into Vue2 apps. So perhaps the best idea is just to press on with that. Vue3 might have to be its own discussion. |
Starting this thread to prevent #12 from getting cluttered.
@Mootook @djmaze
The text was updated successfully, but these errors were encountered: