07.配置pinia

  1. 安装相关包

npm install pinia

  1. 配置main.ts,新增如下内容
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

import {createApp} from 'vue'
import '@/style.css'
import App from '@/App.vue'
import router from "@/router";

import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

// 新增内容
import {createPinia} from "pinia";

const app = createApp(App)

// 新增内容
app.use(router).use(ElementPlus).use(createPinia())

// 新增内容 全局注册图标组件
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

app.mount('#app')

  1. 新建store目录,并在store目录下新建index.ts文件,文件内容如下:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11

import {defineStore} from "pinia";

export const useMainStore = defineStore('main', {
    state: () => {
        return {}
    },
    getters: {},
    actions: {}
})