Use when creating a Pinia store using the setup (composition) syntax with TypeScript.
Generate a Pinia store using the setup (composition) syntax with TypeScript.
When the user asks to create a store, manage client-side state, or add a Pinia store.
filter, cart, sidebar)Create at resources/js/stores/use{Name}Store.ts:
import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
export const use{Name}Store = defineStore('{name}', () => {
// State
// Computed (getters)
// Actions
return {
// Expose state, getters, and actions
}
})
resources/js/stores/useFilterStore.ts
import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
export const useFilterStore = defineStore('filter', () => {
const search = ref('')
const sortBy = ref('name')
const sortDirection = ref<'asc' | 'desc'>('asc')
const perPage = ref(15)
const hasActiveFilters = computed(() => {
return search.value !== '' || sortBy.value !== 'name' || sortDirection.value !== 'asc'
})
function toggleSort(column: string) {
if (sortBy.value === column) {
sortDirection.value = sortDirection.value === 'asc' ? 'desc' : 'asc'
} else {
sortBy.value = column
sortDirection.value = 'asc'
}
}
function reset() {
search.value = ''
sortBy.value = 'name'
sortDirection.value = 'asc'
perPage.value = 15
}
return { search, sortBy, sortDirection, perPage, hasActiveFilters, toggleSort, reset }
})
use{Name}Store patterndefineStore) is the camelCase name