pinia优化重复请求

目录

1 前言

2 使用步骤

2.1 安装pinia

 2.2 定义一个store

2.3 在父组件即index.vue中调用方法

2.4 子组件使用获取到的值


1 前言

在我们的页面中常常存在多个组件,多个组件可能会对某个接口发送多次请求,为了避免这种情况,我们就需要使用pinia来解决该问题。

2 使用步骤

2.1 安装pinia

在命令提示符窗口,输入如下指令安装pinia:

npm install pinia

 2.2 定义一个store

 假设我们需要获取一个分类列表,这就是被我们重复调用的部分,现在我们先定义一个store,包含获取List的方法和List。

//导入pinia
import { defineStore } from 'pinia'
import {ref} from "vue";
//导入我们自定义的API接口
import {getCategoryAPI} from "@/apis/layout.js";

//'category'为独一无二的名字
export const useCategoryStore = defineStore('category', () => {
    //需要的值
    const categoryList = ref([])
    //获取List的方法
    const getCategory = async () => {
        const res = await getCategoryAPI()
        categoryList.value = res.result
    }
    //返回方法和所需List
    return {
        categoryList,
        getCategory
    }
})

2.3 在父组件即index.vue中调用方法

<script setup>
//其它代码
//导入我们刚才定义的store
import {useCategoryStore} from "@/stores/category.js";
import {onMounted} from "vue";

const categoryStore = useCategoryStore()

//调用方法获取List
onMounted(() => {
  categoryStore.getCategory()
})
</script>

<template>
  <!--其它子组件-->
  <LayoutHeader />
  <RouterView />
  <LayoutFooter />
</template>

2.4 子组件使用获取到的值

<script setup>
//使用pinia中的数据
import {useCategoryStore} from "@/stores/category.js";
const categoryStore = useCategoryStore()
</script>

<template>
    <!--其它代码-->
</template>


<style scoped lang='scss'>
//其它代码
</style>
  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Pinia中封装axios请求头可以通过在store中定义一个action来实现。首先,在store中引入axios库,并在action中使用axios发送请求。然后,在请求中设置请求头。以下是一个示例代码: ```javascript import { defineStore } from 'pinia'; import axios from 'axios'; export const useUserInfoStore = defineStore('UserInfo', { state: () => ({ // 状态数据 }), actions: { async fetchData() { try { const response = await axios.get('https://api.example.com/data', { headers: { // 设置请求头 Authorization: 'Bearer your_token', 'Content-Type': 'application/json', }, }); // 处理响应数据 // ... } catch (error) { // 处理错误 // ... } }, }, }); ``` 在上述代码中,我们在`fetchData` action中使用axios发送GET请求,并在请求中设置了请求头。你可以根据自己的需求修改请求方法和请求头的内容。 请注意,这只是一个示例代码,你需要根据自己的实际情况进行修改和适配。 #### 引用[.reference_title] - *1* *2* [用Pinia在vue3中实现axios请求](https://blog.csdn.net/qq_49668888/article/details/130981307)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [vue3+ts+axios+pinia实现无感刷新](https://blog.csdn.net/weixin_73184582/article/details/129518773)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

厂里英才

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值