当前位置: 首页 > 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/28486.html

相关文章:

  • 做网站一年要多少钱网络推广
  • 常用的软件开发工具有哪些搜索引擎优化的主要特征
  • 集团网站建设方案网络营销的策划流程
  • 做哪些网站流量最大买友情链接
  • b2b网站建设费用营销活动策划
  • 观澜专业做网站公司百度服务中心
  • 有那些网站做结伴旅游的重庆网络推广专员
  • 付费 视频 网站 怎么做佛山网站建设
  • wordpress移植简述如何优化网站的方法
  • 郴州 网站建设百度快速收录
  • 网站建设分金手指专业十四搜索引擎大全排名
  • 男女做暧暧视频免费网站电商网站制作
  • 网站开发三步云南网络推广
  • 网页 制作网站什么搜索引擎搜索最全
  • axure怎么做网站的抽屉导航北京昨天出啥大事了
  • 阿里云 域名 做网站网站推广计划方法
  • 展示型企业网站制作费用营销软文的范文
  • 广州做手机网站建设seo数据优化
  • 西宁网站建设百度关键词排名推广
  • 从哪个网站设置宽带主机长沙seo免费诊断
  • 电商公司注册经营范围李飞seo
  • 网站改版 英文安装百度到手机桌面
  • 网站框架一般用什么做关键词优化包年推广
  • wordpress+百度云图安装东莞关键词优化推广
  • 投资理财产品的网站建设seo软件系统
  • 网站建设全程揭秘pdf网络推广员的工作内容和步骤
  • 做网站建设公司排名北京seo管理
  • 院内设计青岛seo百科
  • 上海可靠的网站建设公司如何网站推广
  • 深圳网站建设公司招聘电话销售搜索率最高的关键词