vue2项目使用pinia

本文介绍了如何在Vue 3项目中使用Pinia作为状态管理工具,并重点讲解了如何在动态路由场景下正确安装和配置Pinia store。作者分享了解决错误'getActivePinia called with no active Pinia'的方法,以及如何避免在路由懒加载时出现问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pinia

pinia官网:https://pinia.web3doc.top/

安装

yarn add pinia
# 或者使用 npm
npm install pinia

vue2项目使用

main.js文件

import Vue from "vue";
import App from "./App.vue";
import router from "./router";
// import store from "./store";
import { createPinia, PiniaVuePlugin } from "pinia";

Vue.use(PiniaVuePlugin);
const pinia = createPinia();
Vue.config.productionTip = false;

new Vue({
  router,
  pinia,
  render: (h) => h(App),
}).$mount("#app");


定义一个store

/pinia/index.js文件

import { defineStore } from "pinia";

// useStore 可以是 useUser、useCart 之类的任何东西
// 第一个参数是应用程序中 store 的唯一 id
export const useStore = defineStore("globaltop", {
  // other options...
  // 推荐使用 完整类型推断的箭头函数
  state: () => {
    return {
      // 所有这些属性都将自动推断其类型
      counter: 0,
      name: "Eduardo",
      isAdmin: true,
      date: "2022-10-01",
    };
  },
  actions: {
    increment(num) {
      this.counter = this.counter + num;
    },
    randomizeCounter() {
      this.counter = Math.round(100 * Math.random());
    },
  },
});


组件中使用

<template>
  <div class="about">
    <h1>This is an about page</h1>
    <button class="button" @click="changeStore">点击改变store</button>
  </div>
</template>
<script>
import { useStore } from "@/pinia/index";
const store = useStore();

export default {
  name: "AboutView",
  components: {},
  data() {
    return {};
  },
  mounted() {
    console.log("about-moubtd", store);
  },
  methods: {
    changeStore() {
      store.increment(4);
    },
  },
};
</script>
<style scoped lang="scss">
.about {
  background-color: green;
  .button {
    // color: yellow;
    height: 30px;
    width: 180px;
    border-width: 1px;
    color: currentColor;
  }
}
</style>


app.js:371 Uncaught Error: [🍍]: getActivePinia was called with no active Pinia. Did you forget to install pinia?
	const pinia = createPinia()
	app.use(pinia)
This will fail in production.

报错,解决方法,路由动态加载

const routes = [
  {
    path: "/",
    name: "layout",
    redirect: "/home",
    component: Layout,
    children: [
      {
        path: "/home",
        name: "home",
        component: HomeView,
      },
      {
        path: "/about",
        name: "about",
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/AboutView.vue"),
      },
    ],
  },
];

改成一下方式就没有报错了

const routes = [
  {
    path: "/",
    name: "layout",
    redirect: "/home",
    component: Layout,
    children: [
      {
        path: "/home",
        name: "home",
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/HomeView.vue"),
      },
      {
        path: "/about",
        name: "about",
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/AboutView.vue"),
      },
    ],
  },
];
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值