pinia 如何引入
时间: 2023-11-16 18:44:58 浏览: 68
要在项目中引入 Pinia,你可以按照以下步骤进行操作:
1. 首先,确保你的项目使用了 Vue 3。
2. 使用 npm 或者 yarn 安装 Pinia:
```shell
npm install pinia
```
或者
```shell
yarn add pinia
```
3. 在你的 Vue 应用程序的入口文件(通常是 `main.js`)中导入和配置 Pinia:
```javascript
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
const app = createApp(App)
const pinia = createPinia()
app.use(pinia)
app.mount('#app')
```
4. 现在,你可以在组件中使用 Pinia。例如,在一个组件中使用 store:
```vue
<template>
<div>
<p>Count: {{ $store.count }}</p>
<button @click="$store.increment()">Increment</button>
</div>
</template>
<script>
import { defineComponent } from 'vue'
import { useStore } from 'pinia'
export default defineComponent({
setup() {
const store = useStore()
return {
store
}
}
})
</script>
```
这样,你就成功地将 Pinia 引入到你的 Vue 3 项目中了。你可以根据自己的需求创建和使用自定义的 store。
阅读全文
相关推荐


















