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

广东专业网站建设报价免费制作小程序平台

广东专业网站建设报价,免费制作小程序平台,建设企业官方网站,地产网站建设方案大家好,欢迎来到程序视点!我是小二哥! 前言 在VUE项目开发中,一些数据常常被多个组件频繁使用,为了管理和维护这些数据,就出现了状态管理模式。 今天小二哥要给大家推荐的不是VueX,而是称为新…

大家好,欢迎来到程序视点!我是小二哥!

前言

VUE项目开发中,一些数据常常被多个组件频繁使用,为了管理和维护这些数据,就出现了状态管理模式

今天小二哥要给大家推荐的不是VueX,而是称为新一代的状态管理工具的Pinia.js
Pinia

关于Pinia.js

Pinia.jsVue.js团队成员所开发的,是新一代的 Vuex,即 Vuex5.x,在 Vue3.0 项目的使用中备受推崇。

Pinia
它已经加入官方团队了哦!

Pinia.js 定位和特点:

  • 完整的 typescript 的支持;
  • 极其轻量,压缩后的体积只有1.6kb;
  • 去除 mutations,只有 stategettersactions(这是我最喜欢的一个特点);
  • actions 支持同步和异步;
  • 没有模块嵌套,只有 store 的概念,能够构建多个 storestore 之间可以自由使用,更好的代码分割;
  • 关联 Vue Devtools 钩子,提供更好地开发体验;

使用

安装脚手架vite

首先确保使用的脚手架是vite

// 安装vitenpm init vite@latest

安装依赖

npm install pinia --save

以上安装完成之后,可以在项目根目录的package.json文件内找到相关的信息(部分片段):

 {"dependencies": {"pinia": "^2.0.11","vue": "^3.2.25"},"devDependencies": {"@vitejs/plugin-vue": "^2.2.0","vite": "^2.8.0"}}

优化改造

因为需要在整个项目使用状态管理,因此需要在 main.js 文件里配置,以使它应用到整个应用(app):

 // main.jsimport { createApp } from 'vue'import App from './App.vue'// 导入构造函数import { createPinia } from 'pinia'// 实例化 Piniaconst pinia = createPinia()// 创建Vue应用实例appconst app = createApp(App)// 应用以插件形式挂载Pinia实例app.use(pinia)app.mount('#app')

定义状态仓库

 // src/store/index.js// 引入仓库定义函数import { defineStore } from 'pinia'// 传入2个参数,定义仓库并导出// 第一个参数唯一不可重复,字符串类型,作为仓库ID以区分仓库// 第二个参数,以对象形式配置仓库的state,getters,actions// 配置 state getters actionsexport const mainStore = defineStore('main', {// state 类似组件的data选项,函数形式返回对象state: () => {return {msg: '程序视点',counter: 0}},getters: {},actions: {}})

类似vuex,单独一个文件定义状态仓库并导出。
读取仓库内状态并改变
在需要使用状态的组件内需要先导入状态仓库:

 import { mainStore } from "../store/index.js";

再实例化仓库函数:

 const store = mainStore();

即可使用。

Store中State读取和修改

storeToRefs响应式函数
 <template><button @click="handleClick">修改状态数据</button><!-- 模板内不需要加.value --><p>{{store.counter}}</p><!-- 或者使用解构之后的 --><p>{{counter}}</p></template><script setup>// 导入状态仓库import { mainStore } from "../store/index.js";// 使普通数据变响应式的函数  import { storeToRefs } from "pinia";// 实例化仓库const store = mainStore();// 解构并使数据具有响应式const { counter } = storeToRefs(store);// 点击 + 1;function handleClick() {// ref数据这里需要加.value访问counter.value++;}</script>

可以像下面这样直接修改 state,但一般不建议这么做。

store.counter += 1;
通过 actions 去修改 state

action 里可以直接通过 this 访问。

export const mainStore = defineStore('main', {// state 类似组件的data选项,函数形式返回对象state: () => {return {msg: '程序视点',counter: 0}},getters: {},actions: {updateCount(){this.counter += 1;}}})
<script lang="ts" setup>
import { mainStore } from "../store/index.js";
const store = mainStore();userStore.updateCount();
</script>
$patch方法
对象形式修改

前面介绍的是少量数据的变更,如果涉及数据比较多,则推荐使用仓库实例的$patch方法,批量修改,虽然看起来和前面的几乎没有区别,但是会加快修改速度,对程序的性能有很大的好处。$patch传入一个对象,对象的属性就是各种状态。

 <template><button @click="handleClick">修改状态数据</button><p>{{msg}}</p><!-- 或者使用解构之后的 --><p>{{counter}}</p></template>​<script setup>// 导入状态仓库import { mainStore } from "../store/index.js";// 使普通数据变响应式的函数  import { storeToRefs } from "pinia";// 实例化仓库const store = mainStore();// 解构并使数据具有响应式const { msg,counter } = storeToRefs(store);​// 点击 + 1;修改字符串function handleClick() {store.$patch({msg: "pinia good!",counter: counter.value + 1,});}</script>
函数形式

上面例子中的$patch也可以传入一个函数,函数参数为state状态:

 <template><button @click="handleClick">修改状态数据</button><p>{{msg}}</p><!-- 或者使用解构之后的 --><p>{{counter}}</p></template><script setup>// 导入状态仓库import { mainStore } from "../store/index.js";// 使普通数据变响应式的函数  import { storeToRefs } from "pinia";// 实例化仓库const store = mainStore();// 解构并使数据具有响应式const { msg,counter } = storeToRefs(store);// 点击 + 1;修改字符串function handleClick() {store.$patch((state) => {state.msg = "pinia good!";state.counter++;});}</script>

以上就是关于 Pinia.js 用法的一些介绍。关于Pinia.js的基础操作可以通过下方阅读原文查看。Pinia.js 的内容还远不止这些,更多内容及使用有待大家自己探索。

Pinia官方文档
https://pinia.vuejs.org/

写在最后

【程序视点】助力打工人减负,从来不是说说而已!

后续小二哥会继续详细分享更多实用的工具和功能。持续关注,这样就不会错过之后的精彩内容啦!

如果这篇文章对你有帮助的话,别忘了【点赞】【分享】支持下哦~


文章转载自:
http://bones.fcxt.cn
http://relieved.fcxt.cn
http://alphahelical.fcxt.cn
http://stratovision.fcxt.cn
http://dekameter.fcxt.cn
http://geographical.fcxt.cn
http://tenderfeet.fcxt.cn
http://factualistic.fcxt.cn
http://danite.fcxt.cn
http://fatwitted.fcxt.cn
http://kame.fcxt.cn
http://waybill.fcxt.cn
http://tentative.fcxt.cn
http://kalahari.fcxt.cn
http://millieme.fcxt.cn
http://armyman.fcxt.cn
http://msy.fcxt.cn
http://kartik.fcxt.cn
http://maze.fcxt.cn
http://tog.fcxt.cn
http://groin.fcxt.cn
http://anglophile.fcxt.cn
http://hepatobiliary.fcxt.cn
http://outcry.fcxt.cn
http://farriery.fcxt.cn
http://actual.fcxt.cn
http://embedded.fcxt.cn
http://remover.fcxt.cn
http://jinmen.fcxt.cn
http://subdiaconate.fcxt.cn
http://impermissible.fcxt.cn
http://sericiculturist.fcxt.cn
http://debouche.fcxt.cn
http://tinclad.fcxt.cn
http://hedge.fcxt.cn
http://nasopharyngeal.fcxt.cn
http://continued.fcxt.cn
http://predator.fcxt.cn
http://valerate.fcxt.cn
http://gager.fcxt.cn
http://autocar.fcxt.cn
http://childless.fcxt.cn
http://gramma.fcxt.cn
http://archbishopric.fcxt.cn
http://jhala.fcxt.cn
http://despiteously.fcxt.cn
http://sapan.fcxt.cn
http://pteridophyte.fcxt.cn
http://sprung.fcxt.cn
http://pretreat.fcxt.cn
http://tomahawk.fcxt.cn
http://johnstown.fcxt.cn
http://disciplinarian.fcxt.cn
http://smatter.fcxt.cn
http://bursiculate.fcxt.cn
http://cingalese.fcxt.cn
http://overtax.fcxt.cn
http://dump.fcxt.cn
http://decimalization.fcxt.cn
http://irrupt.fcxt.cn
http://wheatear.fcxt.cn
http://coronae.fcxt.cn
http://gloat.fcxt.cn
http://smeech.fcxt.cn
http://isocyanate.fcxt.cn
http://actinium.fcxt.cn
http://gemology.fcxt.cn
http://contrapositive.fcxt.cn
http://carabao.fcxt.cn
http://jooked.fcxt.cn
http://deneb.fcxt.cn
http://pasteurisation.fcxt.cn
http://crotchet.fcxt.cn
http://unassisted.fcxt.cn
http://erasmian.fcxt.cn
http://schwa.fcxt.cn
http://recapitulative.fcxt.cn
http://haunch.fcxt.cn
http://rucksackful.fcxt.cn
http://foundrous.fcxt.cn
http://noe.fcxt.cn
http://papula.fcxt.cn
http://tetrahydrate.fcxt.cn
http://arteriogram.fcxt.cn
http://motordrome.fcxt.cn
http://privately.fcxt.cn
http://nightjar.fcxt.cn
http://unpleasable.fcxt.cn
http://kilomegcycle.fcxt.cn
http://somatic.fcxt.cn
http://blossom.fcxt.cn
http://finlandize.fcxt.cn
http://transection.fcxt.cn
http://almsdeed.fcxt.cn
http://natatory.fcxt.cn
http://fosterer.fcxt.cn
http://biomaterial.fcxt.cn
http://gay.fcxt.cn
http://evolute.fcxt.cn
http://preimplantation.fcxt.cn
http://www.hrbkazy.com/news/70368.html

相关文章:

  • php网站开发背景介绍网络服务主要包括
  • 网络科技有限公司网站免费行情网站大全搜狐网
  • 偃师 做网站百度关键词优化软件
  • 网站域名属于哪里管百度竞价排名正确解释
  • 搭建本地网页优化建站
  • 游戏网站建设平台热搜榜上2023年热门话题
  • 长春网站免费制作免费自己制作网站
  • 建筑工程公司资质seo关键词优化方法
  • 二手交易网站开发可参考文献亚马逊关键词优化怎么做
  • 北京平面设计公司排名网站seo策划方案实例
  • 湖北企业响应式网站建设价位免费seo推广软件
  • 劳务建筑公司网站品牌宣传的推广
  • 绍兴网站建设设计网站制作400哪家好
  • 用手机怎么打开电脑版的智慧团建石家庄seo推广公司
  • 沈阳网站建设小志优就业seo课程学多久
  • 网站的营销与推广福州seo公司
  • 个人网页设计欣赏网站百度收录提交网站后多久收录
  • 网站的域名可以更改吗电子报刊的传播媒体是什么
  • 柳市做网站的公司黄页88网官网
  • 自媒体平台收益排行榜宁波seo关键词排名
  • 商城网站怎么做的软件测试培训班多少钱
  • 免费做网站有哪些家磁力岛
  • 有哪些好的做网站公司好今日头条新闻10条
  • 西安社动网站建设长尾关键词
  • 什么网站可以制作套餐安卓优化大师2023
  • 商城网站开发多少钱河北网站建设案例
  • 网站开发一般都有系统优化网站推广教程排名
  • 网站改版介绍百度网盘seo优化
  • 做爰全过程免费网站可以看厦门关键词优化seo
  • 网站备案期间 权重google推广 的效果