pinia的使用

安装

yarn add pinia
或者使用 npm
npm install pinia

pinia的基本使用

state

大多数时候,state 是 store 的核心部分。 我们通常从定义应用程序的状态开始。 在 Pinia 中,状态被定义为返回初始状态的函数。 Pinia 在服务器端和客户端都可以工作。

import { createApp } from "vue";
import "./style.css";
import App from "./App.vue";
import { createPinia } from "pinia";

const app = createApp(App);

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

创建一个store文件
在这里插入图片描述

import { defineStore } from "pinia";

export const useCounterStore = defineStore("counterStore", {
  state: () => {
    return {
      counter: 0,
    };
  },
});

测试pinia

<template>
  <div></div>
</template>

<script setup>
import { useCounterStore } from "../store/counterStore";
const counterStore = useCounterStore();
console.log(counterStore);
</script>

在这里插入图片描述
打印结果如下:
在这里插入图片描述

action

Actions 相当于组件中的 methods。 它们可以使用 defineStore() 中的 actions 属性定义,并且它们非常适合定义业务逻辑:

import { defineStore } from "pinia";

export const useCounterStore = defineStore("counterStore", {
  state: () => {
    return {
      counter: 0,
    };
  },
  actions: {
    increment(val) {
      console.log(val);
      this.counter++;
    },
  },
});
<template>
  <div>
    <!-- {{ counterStore.counter }} -->
    {{ counter }}
    <button @click="add()">点击增加</button>
  </div>
</template>

<script setup>
import { useCounterStore } from "../store/counterStore";
import { storeToRefs } from "pinia";
// 方式一
// const counterStore = useCounterStore();
// const add = () => {
//   counterStore.increment("增加counter");
// };
// 方式二
const counterStore = useCounterStore();
// 让counter变得响应式
const { counter } = storeToRefs(counterStore);
const increment = counterStore.increment;

const add = () => {
  counterStore.increment("增加counter");
};
</script>


效果如图:
在这里插入图片描述

优化一下路径

打开vite.config.js配置文件

使用相对路径时太多"…/…/",看起来不美观
import { useCounterStore } from “…/store/counterStore”;
变为
import { useCounterStore } from “@/store/counterStore”;

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
});

json-server模拟数据

npm install json-server -g
在这里插入图片描述
启动json-server
json-server .\vite-project\src\data\api.json -p 9000

启动成功
在这里插入图片描述
安装axios

npm i axios -S

import { defineStore } from "pinia";
import axios from "axios";

export const useCounterStore = defineStore("counterStore", {
  state: () => {
    return {
      products: [],
    };
  },
  actions: {
    async loadData() {
      let result = await axios.get("http://localhost:9000/data");
      console.log("result:", result);
      this.products = result.data;
    },
  },
});

<template>
  <div>
    <ul>
      <li v-for="item in products">
        {{ item.name }}
      </li>
    </ul>
    <button @click="add()">点击请求数据</button>
  </div>
</template>

<script setup>
import { useCounterStore } from "@/store/counterStore";
import { storeToRefs } from "pinia";

const counterStore = useCounterStore();
const { products } = storeToRefs(counterStore);
const loadData = counterStore.loadData;

const add = () => {
  loadData();
};
</script>

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值