## Auto imports
-By default `@pinia/nuxt` exposes one single auto import: `usePinia()`, which is similar to `getActivePinia()` but works better with Nuxt. You can add auto imports to make your life easier:
+By default `@pinia/nuxt` exposes a few auto imports:
-```js
-// nuxt.config.js
+- `usePinia()`, which is similar to `getActivePinia()` but works better with Nuxt. You can add auto imports to make your life easier:
+- `defineStore()`
+- `acceptHMRUpdate()` for [hot module replacement](../cookbook/hot-module-replacement.md)
+
+It also automatically imports **all stores** defined withing your `stores` folder. It doesn't lookup for nested stores though. You can customize this behavior by setting the `storeDirs` option:
+
+```ts
+// nuxt.config.ts
export default defineNuxtConfig({
// ... other options
modules: ['@pinia/nuxt'],
pinia: {
- autoImports: [
- // automatically imports `defineStore`
- 'defineStore', // import { defineStore } from 'pinia'
- ['defineStore', 'definePiniaStore'], // import { defineStore as definePiniaStore } from 'pinia'
- ],
+ storeDirs: ['./stores/**', './custom-folder/stores/**'],
},
})
```
+Note the folders are relative to the root of your project. If you change the `srcDir` option, you need to adapt the paths accordingly.
+
## Nuxt 2 without bridge
Pinia supports Nuxt 2 until `@pinia/nuxt` v0.2.1. Make sure to also install [`@nuxtjs/composition-api`](https://composition-api.nuxtjs.org/) alongside `pinia`:
* directly adding the dirs to the `imports.dirs` option. If you want to
* also import nested stores, you can use the glob pattern `./stores/**`
*
- * @default `['./stores']`
+ * @default `['stores']`
*/
storesDirs?: string[]
}
},
defaults: {
disableVuex: true,
- storesDirs: ['./stores'],
},
setup(options, nuxt) {
const resolver = createResolver(import.meta.url)
{ from: composables, name: 'usePinia' },
])
+ if (!options.storesDirs) {
+ // resolve it against the src dir which is the root by default
+ options.storesDirs = [resolver.resolve(nuxt.options.srcDir, 'stores')]
+ }
+
if (options.storesDirs) {
for (const storeDir of options.storesDirs) {
addImportsDir(resolver.resolve(nuxt.options.rootDir, storeDir))