vue3中使用pinia实现数据共享

场景:地区选择器中,由于方法很长,加载的数据很多,导致页面每进去一次都要加载很久,造成页面卡顿。所以想要把这个数据和方法,提到外面,得到的数据,供所有页面都能正常使用。

使用pinia:

1.main.ts中

import { createPinia } from 'pinia'

2.stores/area.js

import { defineStore } from 'pinia'
import mapData from '../assets/mapData/area.json';
export const useArea = defineStore({
    id: 'area',
    state: () => {
        return {
            // navInfo: []
            province: [],
            cities : [],
            counties : []
        }

    },
    getters: {

    },
    actions: {
         getCityData(){
          var that = this
          // var data = mapData
          that.province = []
          that.cities = []
          that.counties = []
            for (var item in mapData) {
              if (item.match(/0000$/)) {
                //省
                this.province.push({ id: item, value: mapData[item], children: [] });
              } else if (item.match(/00$/)) {
                //市
                this.cities.push({ id: item, value: mapData[item], children: [] });
              } else {
                //区
                this.counties.push({ id: item, value: mapData[item] });
              }
            }
            // 分类市级
            for (var index in this.province) {
              for (var index1 in this.cities) {
                if (
                    this.province[index].id.slice(0, 2) ===
                    this.cities[index1].id.slice(0, 2)
                ) {
                    this.province[index].children.push(this.cities[index1]);
                }
              }
            }
        
            // 分类区级
            for (var item1 in this.cities) {
              for (var item2 in this.counties) {
                if (
                    this.counties[item2].id.slice(0, 4) ===
                    this.cities[item1].id.slice(0, 4)
                ) {
                    this.cities[item1].children.push(this.counties[item2]);
                }
              }
            }
          },
    }
})

 3.页面中正常使用。(如果想要数据不互相影响,那就注释,如果想要共享,那就打开注释的代码。

// import { storeToRefs } from "pinia";
import { useArea } from "@/stores/area";
const areaStore = useArea();
// const { province,cities,counties } = storeToRefs(areaStore);
import mapData from "@/assets/mapData/area.json"
   province:areaStore.province,
   cities: areaStore.cities,
   counties: areaStore.counties,

pinia相关的一篇链接:(69条消息) Vue3-Pinia的基本使用_blackzjj的博客-CSDN博客_vue3 pinia

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的示例代码,可以实现一个类似网易云音乐app的圆形的音乐播放进度条: 首先,我们需要在 Vue 组件引入 Canvas 元素,并在组件定义一个 Canvas 上下文对象: ```html <template> <div> <canvas ref="canvas" width="200" height="200"></canvas> </div> </template> <script> export default { name: 'Progress', mounted() { this.canvas = this.$refs.canvas this.ctx = this.canvas.getContext('2d') }, methods: { drawProgress(percent) { // 绘制圆形进度条 const centerX = this.canvas.width / 2 const centerY = this.canvas.height / 2 const radius = 80 const startAngle = -Math.PI / 2 const endAngle = startAngle + (Math.PI * 2) * percent this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height) this.ctx.beginPath() this.ctx.arc(centerX, centerY, radius, startAngle, endAngle) this.ctx.strokeStyle = '#fff' this.ctx.lineWidth = 10 this.ctx.stroke() } } } </script> ``` 接下来,我们可以通过传递一个百分比值来绘制进度条: ```html <template> <div> <canvas ref="canvas" width="200" height="200"></canvas> <div>{{ percent }}%</div> </div> </template> <script> export default { name: 'Progress', props: { percent: { type: Number, required: true, default: 0 } }, mounted() { this.canvas = this.$refs.canvas this.ctx = this.canvas.getContext('2d') this.drawProgress(this.percent / 100) }, watch: { percent(newValue) { this.drawProgress(newValue / 100) } }, methods: { drawProgress(percent) { // 绘制圆形进度条 const centerX = this.canvas.width / 2 const centerY = this.canvas.height / 2 const radius = 80 const startAngle = -Math.PI / 2 const endAngle = startAngle + (Math.PI * 2) * percent this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height) this.ctx.beginPath() this.ctx.arc(centerX, centerY, radius, startAngle, endAngle) this.ctx.strokeStyle = '#fff' this.ctx.lineWidth = 10 this.ctx.stroke() } } } </script> ``` 这样,我们就可以在组件传递一个 `percent` 属性,来实现动态绘制进度条的效果了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值