Skip to content

Commit

Permalink
fix(Vue-vuex): computed value from vuex state
Browse files Browse the repository at this point in the history
- Wrap `useStore` to `useAppStore` for types inference.
- Passing computed value from vuex state to template.
  • Loading branch information
sabertazimi committed Sep 27, 2021
1 parent 4d2cd90 commit dab0c7b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
6 changes: 4 additions & 2 deletions packages/vue-trello/src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InjectionKey } from 'vue';
import { createStore, Store } from 'vuex';
import { createStore, useStore, Store } from 'vuex';
import { getDefaultBoard } from 'src/services';
import type { BoardType } from 'src/services';

Expand All @@ -19,6 +19,8 @@ const store = createStore<State>({
modules: {},
});

export { key };
const useAppStore = () => useStore<State>(key);

export { key, useAppStore };
export type { State };
export default store;
4 changes: 4 additions & 0 deletions packages/vue-trello/src/views/Board.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ import HelloWorld from 'src/components/HelloWorld.vue';
<router-link class="router-link" :to="{ name: 'task', params: { id: 123 } }">
To task
</router-link>
|
<router-link class="router-link" :to="{ name: 'board' }">
Back home
</router-link>
<router-view />
</template>
16 changes: 15 additions & 1 deletion packages/vue-trello/src/views/Task.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
<script setup lang="ts">
import { computed } from 'vue';
import { useAppStore } from 'src/store';
defineProps<{ id: string }>();
const store = useAppStore();
const columns = computed(() => store.state.board.columns);
</script>

<template>
<div>Task View {{ id }}</div>
<div v-if="columns">
<div v-for="column in columns" :key="column.name">
<ul v-if="column.tasks">
<li v-for="task in column.tasks" :key="task.id">
{{ task.name }}
</li>
</ul>
</div>
</div>
</template>

0 comments on commit dab0c7b

Please sign in to comment.