vue3使用pinia

在main.js注册pinia


import {createPinia} from "pinia";

import "./permission";

const pinia=createPinia()

app.use(pinia)

// store app.ts


import {defineStore} from "pinia"

interface IState {

  projectType:string

}

export const useAppStore = defineStore({

  id:"apps",

  state: ():iState=>({

    projectType :"aaa"

}),

action:{

  setProjectType(projectType:string) {

  this.projectType = projectType

},

},

});

//permiss.js


import router from "./router";

import {useAppStore} from "@/srores/apps";

router.beforeEach(async (to ,from,next)=>{

  const apps = useAppStore()

  let projectType= to.query.projectType?to.query.projectType :"aaa"

apps.setProjectType(projectType)

// 路由发生变化修改页面title

if(to.meta.title){

  document.title=to.meta.title

}

next();

})

//页面使用


import {useAppStore} from "@/srores/apps";

  const apps = useAppStore()

const projectType = computed({

    get(){

    retun apps.projectType

    },

  set(){}

  })

你可能感兴趣的:(vue3使用pinia)