Skip to content
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

Examples for pulling state/actions/etc into components #10

Open
hworld opened this issue Apr 19, 2017 · 2 comments
Open

Examples for pulling state/actions/etc into components #10

hworld opened this issue Apr 19, 2017 · 2 comments

Comments

@hworld
Copy link

hworld commented Apr 19, 2017

Curious if you had examples for how you pull vuex stores into your components for state access committing and dispatching?

Do you define $store as your typed store for each component that may use store stuff?

@snaptopixel
Copy link
Owner

Thanks @hworld... I'm working on more/better examples that I hope to have done in a couple of weeks.

@hworld
Copy link
Author

hworld commented Apr 21, 2017

Sounds great!

Not sure if you plan on writing out custom code/decorators for pulling state/actions/getters/mutations into components (wrappers around mapGetters, etc.) but I'm currently using https://github.com/ktsn/vuex-class/ and here's how I did it in my components.

type Actions = {
	bootstrap: any,
};

type Mutations = {
	bootstrapGame: number;
};

@module({
	store: true,
})
export class AppStore
{
	dispatch: VuexDispatch<Actions>;
	commit: VuexCommit<Mutations>;
	game: Game | null = null;

	get packages()
	{
		return [];
	}

	@action
	async bootstrap( payload: Actions['bootstrap'] )
	{
		// do something
	}

	@mutation
	bootstrapGame( gameId: Mutations['bootstrapGame'] )
	{
		// do something
	}
}

class MyComponent
{
	@State game: AppStore['game'];
	@Getter packages: AppStore['packages'];
	@Action bootstrap: AppStore['bootstrap'];
	@Mutation bootstrapGame: AppStore['bootstrapGame'];
}

This ensures proper types for everything so if I change the AppStore all my components will have correct type/error checking. Maybe it'll help for the examples! =]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants