Skip to content
On this page

Pinia 介绍

Pinia最初是在2019年11月左右使用Composition API重新设计Vue Store的一个试验。从那时起,最初的原则仍然相同,但Pinia同时适用于Vue 2Vue 3,并且不要求您使用Composition API。除了安装和SSR之外,两者的API都是相同的,这些文档针对Vue 3,并在必要时提供有关Vue 2的注释,以便Vue 2Vue 3的用户都可以阅读!

安装

使用 npmyarn 都可以进行安装, 可以选择一个你最熟悉的方式去安装.

bash
$ yarn add pinia --save
$ yarn add pinia --save

或者使用 npm

bash
$ npm install pinia --save
$ npm install pinia --save

提示

This is a tip

js
export default { // Highlighted
  data () {
    return {
      msg: `Highlighted!
      This line isn't highlighted,
      but this and the next 2 are.`,
      motd: 'VitePress is awesome',
      lorem: 'ipsum',
    }
  }
}
export default { // Highlighted
  data () {
    return {
      msg: `Highlighted!
      This line isn't highlighted,
      but this and the next 2 are.`,
      motd: 'VitePress is awesome',
      lorem: 'ipsum',
    }
  }
}

引用Pinia

Pinia进入到vue3项目中. 修改main.js文件. 组件工API的方式将其引入到项目中.

js
import { createApp } from "vue";
import { createPinia } from "pinia";

const app = createApp(App);

app.use(createPinia());
app.mount("#app");
import { createApp } from "vue";
import { createPinia } from "pinia";

const app = createApp(App);

app.use(createPinia());
app.mount("#app");