当前位置: 首页 > news >正文

成都网站建设金网科技设计个人网站

成都网站建设金网科技,设计个人网站,注册域名的网站有哪些,做网站只有域名1、Pinia基本概念 1、Pinia向外暴露了几个重要的函数,分别是createPinia、defineStore和storeToRefs 2、pinia有五个核心管理概念: store、store、getters、action、plugins 2、Pinia基本使用 1、安装过程 1、安装pinia插件:npm install pini…

1、Pinia基本概念

1、Pinia向外暴露了几个重要的函数,分别是createPinia、defineStore和storeToRefs

2、pinia有五个核心管理概念:

        store、store、getters、action、plugins

2、Pinia基本使用

1、安装过程

1、安装pinia插件:npm install pinia --save//在main。js中引入,需要调用createPinia()方法将pinia实例化,然后挂载到vue实例上就可以了
2、// 创建pinia状态管理库
import { createPinia } from 'pinia'
const pinia = createPinia()app.use(pinia)

2、简单使用。

defineStore函数用来创建生成store对象的函数,它接收两个参数:第1个参数是store对象的唯一标识,必须保证多个不同的store对象的标识是唯一的;第2个参数是配置对象,基本配置包括state、getters和actions。Pinia要求state配置对象必须是返回state对象的箭头函数,getters是包含多个计算属性的对象,而actions是包含多个action方法的对象。

  2、创建store文件夹,并在其中创建index.js文件,内容如下:需要指定唯一的标识符,这里使用useStore作为标识符:import { defineStore } from 'pinia'export const useStore = defineStore('main', {state: () => ({count: 0}),getters: {doubleCount: (state) => state.count * 2},actions: {increment(state) {state.count++}}})

3、持久化

1、store中的数据,刷新页面后就丢失了,如果想保留这些数据,就要用到数据持久化了。

推荐使用pinia-plugin-persistedstate

2、安装插件

 npm install pinia-plugin-persistedstate

3、引入持久化插件


在mai.js// 创建pinia状态管理库
import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'const pinia = createPinia()
// 引用插件
pinia.use(piniaPluginPersistedstate)

4、开启持久化数据

最后对要实现状态数据持久化的Pinia模块进行持久化的开启配置,比如我们要对计数器模块的count进行持久化处理,只需在counter.js文件中添加persist为true的配置即可。

4、举个例子

1、在store中定义一个一个counter.js文件

const useCounterStore = defineStore('counter', {state: () => ({count: 0,countList: []}),actions: {increment(num) {this.count++;this.countList.push(this.count);},decrement(num) {this.count--;this.countList.push(this.count);},reset() {this.count = 0;this.countList = [];},save() {localStorage.setItem('count', JSON.stringify(this.count));localStorage.setItem('countList', JSON.stringify(this.countList));},},getters: {showCount: (state) => state.count,showCountList: (state) => state.countList,},persist: true,
});export default useCounterStore;

2、在vue组件中使用

<template>
<div>{{ store.count }}
</div>
<div>{{store.showCount}}
</div><div>{{store.countList}}</div><el-button @click="store.increment">+</el-button><el-button @click="store.decrement">-</el-button><el-button @click="store.reset">异步+</el-button>
</template>
<script setup>import useCounterStore from "@/storespinia/counter";
const store = useCounterStore();</script>

5、模块汇总化

1、其中pinia中可以自己归类于一个模块,可以互相不影响,但是也可以通过模块化将相关的模块归集在一起,进行模块的处理化

2、新建立一个index.js

// 统一导出userstore模块
import useCounterStore from "@/storespinia/counter.js";
import userStoreLogin from "@/storespinia/user.js";export default function userStore() {return {counter:useCounterStore(),userLogin:userStoreLogin(),}
}

3、引用模块化

<div>{{ ddd.count }}
</div>
<div>{{ddd.showCount}}
</div><el-button @click="ddd.increment">+</el-button><el-button @click="ddd.decrement">-</el-button><el-button @click="ddd.reset">异步+</el-button><el-button @click="ddd.double">同步+</el-button></template>
<script setup>import userStore from "@/storespinia/index";const store = userStore();
const ddd = store.counter;</script>

http://www.shuangfujiaoyu.com/news/60046.html

相关文章:

  • 兴隆大院网站哪个公司做的百度网盘客户端下载
  • 网站开发与设计难嘛百度网页版浏览器入口
  • 创业网站模板百度商家版下载
  • 网站微信支付怎么做的产品推广方式及推广计划
  • iframe wordpress郑州优化网站关键词
  • 网站开发php岗位职责大数据分析培训机构
  • 中国新冠疫苗接种率树枝seo
  • 网站推广和seo网站生成器
  • 盗图来做网站北京企业网站推广哪家公司好
  • jsp动态网站开发考试题营销模式和营销策略
  • 浦东网站建设公司在线咨询
  • 做网站的文案怎么写seo搜索引擎优化哪家好
  • 网站建设色彩设计有什么用广点通
  • 没有网站服务器空间如何用ftpseo搜索推广
  • 如何建设社区网站网站推广优化排名seo
  • 东莞网站优化公司搜索引擎费用
  • 广州网站建设clov5r网站推广引流最快方法
  • 党政机关门户网站建设网站排名优化价格
  • 重庆电商网站建设制作一个网站需要多少费用
  • 怎么做扒代码网站百度搜索排名服务
  • 成都网站建设哪儿济南兴田德润怎么联系郑州制作网站公司
  • 网站建设的特点友情链接有用吗
  • 石家庄有哪些公司可以做网站微信群推广网站
  • 微网站制作十大免费网站推广入口
  • 华亭县门户网站最简短的培训心得
  • 山西做网站的企业百度竞价排名服务
  • 谎称在赌博网站做维护seo是什么意思的缩写
  • 宝鸡网站建设排行榜google推广方式和手段有哪些
  • 河南seo公司企业网站seo案例分析
  • 武汉网站快速排名提升seo百度站长工具查询