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

河北省企业网站建设公司杭州网站优化

河北省企业网站建设公司,杭州网站优化,网站怎么做速排,dw怎么做jsp网站自研 Petty 状态管理库产生背景 petty 是一款适用于 vue2.5以下版本(目前已兼容vue2.5x 以上版本)的状态管理库,能够在 vue 2这种配置项的代码中,去实现类似于 vue3 里的 pinia、React 里的hook的调用形式,用函数式的…

自研 Petty 状态管理库产生背景

  • petty 是一款适用于 vue2.5以下版本(目前已兼容vue2.5x 以上版本)的状态管理库,能够在 vue 2这种配置项的代码中,去实现类似于 vue3 里的 pinia、React 里的hook的调用形式,用函数式的编程思想去代替掉 vuex 这样的配置项的状态管理库,能让开发者在 vue2 里面尽情拥抱 hook
  • 整个 petty 的灵感来源于 Hox 状态管理库(一款 React 的函数式状态管理),既然 React 可以,谁说 Vue 就不行呢

Petty 状态管理库的优势

  • 支持 SSR,全部代码采用实例化的形式编写,在服务端渲染时返回的是一个实例,多请求不会造成状态污染
  • 代码库体积非常小,正如它的命名 petty(小巧精致)
  • 兼容 vuex 写法以及 vue3 的 pinia 写法,使用起来没有额外心智负担
  • 语法糖采用 vue3 里的 ref 形式,在后续升级过渡中不会带来额外心智成本
  • 函数式的状态管理库,随处定义随处使用
    • 通过将一个个状态变成一个函数,打破 vuex 配置项的管理方式,让代码更加可拆分以及可维护
    • 通过函数合并多个状态,符合人类思维
    • 在vue2里面去实现类似 pinia、hook 这样的函数式编程,是 petty 设计的最大初衷
  • 支持多种调用方式,你的 hook 状态管理库,不仅仅只能通过 hook
  • 函数式天然支持异步,不再需要 action 和 mutation
  • 支持 vuex 、pinia 的各种 helper 辅助开发
  • 支持解构赋值

使用介绍

定义状态 store

  • 是的,你返回的内容,就是你想需要共享的状态
  • 是的,定义一个状态管理库就这么简单,无需额外配置项
  • 是的,异步代码就和正常写异步代码一样,无需其它状态管理库中的 action、effect、redux-thunk 之类的额外配置
import {createStore} from 'petty';const myStore = createStore('myStore', function ({$patch, $active}) {const address = $active('somewhere');const age=12;function changeAddress() {address.value = 'right here';}return {name: 'jeff',age,address,changeAddress,};
});export default myStore;// 代码里使用
import myStore from './stroe/myStore';
const [store, dispatch] = myStore();<template><div id="app"><div>{{ store.name }}</div><div>{{ store.address.value }}</div></div>
</template>export default defineComponent({name: 'App',computed: {store: () => store,},mounted() {setTimeout(() => {dispatch({name: 'jack', address: 'wheee'});store.changeAddress();}, 1000);},
});
</script>

修改状态

直接修改(不推荐)
  • 在 store 挂载的数据会被放到 vue 实例上,所以支持直接修改,但不推荐,这样会打破 flux 这类的单向数据流的模式
import myStore from './stroe/myStore';
const [store, dispatch] = myStore();<template><div id="app"><div>{{ store.name }}</div><div>{{ store.address.value }}</div></div>
</template>export default defineComponent({name: 'App',computed: {store: () => store,},mounted() {setTimeout(() => {store.name='xxx'store.address.value='xxx'}, 1000);},
});
</script>
通过dispatch修改(类 React 方式)
<script>
import myStore from './store/myStore';
const [store, dispatch] = myStore();export default {mounted() {setTimeout(() => {dispatch({name: 'xxx', address: 'xxx'});}, 2000);},
}
</script>
通过函数修改(类 vuex 、redux方式)
import {createStore} from '../pettyStore';
const myStore = createStore('myStore', function ({$patch, $active}) {const currentIndex = $active(0);const date = $active('2020年5月1日');const computedDate = function () {return `${date.value} -'今日'${Date().toLocaleLowerCase()}`;};const changeDate = dateStr => {date.value = dateStr;};return {name: 'myStore',currentIndex,date,computedDate,changeDate,};
});export default myStore;// 代码中
<script>
import myStore from './store/myStore';
const [store, dispatch] = myStore();export default {mounted() {setTimeout(() => {store.changeDate(countDate(index));}, 2000);},
}
</script>

修改 immutable 状态

  • 对于在函数里面直接修改 string、number 这样的基础类型,为了让修改可以响应式,需要使用 $active 包裹,然后使用 .value 的形式改动,和 vue3 的 ref 语法糖一致
import {createStore} from '../pettyStore';
const myStore = createStore('myStore', function ({$patch, $active}) {const currentIndex = $active(0);const date = $active('2020年5月1日');const computedDate = function () {return `${date.value} -'今日'${Date().toLocaleLowerCase()}`;};const changeDate = dateStr => {date.value = dateStr;};return {name: 'myStore',currentIndex,date,computedDate,changeDate,};
});export default myStore;

定义 store 计算属性

  • 定义计算属性也非常简单,直接返回函数即可
import {createStore} from '../pettyStore';
const myStore = createStore('myStore', function ({$patch, $active}) {const currentIndex = $active(0);const date = $active('2020年5月1日');const computedDate = function () {return `${date.value} -'今日'${Date().toLocaleLowerCase()}`;};const changeDate = dateStr => {date.value = dateStr;};return {name: 'myStore',currentIndex,date,computedDate,changeDate,};
});export default myStore;// 代码中
<template><div class="pie-cont"><div class="pie-date2">详细日期{{ computedDate }}</div></div>
</template>
<script>
import myStore from './store/myStore';
const [store, dispatch] = myStore();export default {computed: {store: () => store,computedDate: store.computedDate,}
}
</script>

异步操作

  • 是的,异步操作也和正常写函数一样完全无感
import {createStore} from '../pettyStore';
const myStore = createStore('myStore', function ({$patch, $active}) {const currentIndex = $active(0);const date = $active('2020年5月1日');const computedDate = function () {return `${date.value} -'今日'${Date().toLocaleLowerCase()}`;};const changeDate = dateStr => {date.value = dateStr;};const changDateAsync = () => {new Promise(resolve => {setTimeout(() => {date.value = '2025年1月1日';resolve();}, 3000);});};return {name: 'myStore',currentIndex,date,computedDate,changeDate,changDateAsync,};
});export default myStore;// 代码里
<script>
import myStore from './store/myStore';
const [store, dispatch] = myStore();export default {mounted() {store.changDateAsync();},
}
</script>

多种方式引入 store

  • 是的,除了能像上面的方式引入 petty
  • 你还能在代码里通过 this.$petty 直接引入
// 代码里
<script>
import myStore from './store/myStore';
const [store, dispatch] = myStore();export default {mounted() {const useStore = this.$petty.stores.get('myStore');const [store,dispatch]=useStore();},
}
</script>

多状态 store 合并

  • 是的,就是函数的导入和组装!
// otherStore
import {createStore} from '../pettyStore';
const myStore = createStore('otherStore', function ({$patch, $active}) {return {otherStoreName: 'other',};
});export default myStore;// myStore
import {createStore} from '../pettyStore';
import otherStore from './otherStore';const myStore = createStore('myStore', function ({$patch, $active}) {const currentIndex = $active(0);const date = $active('2020年5月1日');const computedDate = function () {return `${date.value} -'今日'${Date().toLocaleLowerCase()}`;};const changeDate = dateStr => {date.value = dateStr;};const getOtherStoreName = () => {const [oStore] = otherStore();return oStore.otherStoreName;};const changDateAsync = () => {new Promise(resolve => {setTimeout(() => {date.value = '2025年1月1日';resolve();}, 3000);});};return {name: 'myStore',currentIndex,date,computedDate,changeDate,changDateAsync,getOtherStoreName,};
});export default myStore;// 代码里
<template><div class="pie-cont"><div class="pie">男女比例饼图:</div><div class="pie-date">日期{{ store.currentIndex.value }}:{{ store.date.value }}</div><div class="pie-date2">详细日期{{ computedDate }}{{ store.getOtherStoreName() }}</div></div>
</template><script>
import myStore from './store/myStore';
const [store, dispatch] = myStore();export default {computed: {store: () => store,computedDate: store.computedDate,},mounted() {const useStore = this.$petty.stores.get('myStore');const [store,dispatch]=useStore();},
}
</script>

使用 mapHelper 简化开发

import { mapActions } from 'petty'export default {// ...methods: {...mapActions(['increment', // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')`// `mapActions` 也支持载荷:'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)`]),...mapActions({add: 'increment' // 将 `this.add()` 映射为 `this.$store.dispatch('increment')`})}
}

卸载 store、重制 store 为初始态

<script>
import myStore from './store/myStore';
const [store, dispatch] = myStore();export default {mounted() {this.$petty.$dispose('myStore')this.$petty.$reset('myStore')},
}
</script>

文章转载自:
http://hauler.cwgn.cn
http://quibbling.cwgn.cn
http://flooey.cwgn.cn
http://deltiologist.cwgn.cn
http://windhover.cwgn.cn
http://demilitarise.cwgn.cn
http://peacoat.cwgn.cn
http://camstone.cwgn.cn
http://physics.cwgn.cn
http://snowpack.cwgn.cn
http://hepatotoxic.cwgn.cn
http://snackery.cwgn.cn
http://ahitophal.cwgn.cn
http://piscataway.cwgn.cn
http://reflecting.cwgn.cn
http://estral.cwgn.cn
http://aristotelian.cwgn.cn
http://labilize.cwgn.cn
http://splendid.cwgn.cn
http://conto.cwgn.cn
http://bridging.cwgn.cn
http://declining.cwgn.cn
http://hexad.cwgn.cn
http://underdraw.cwgn.cn
http://pyrocondensation.cwgn.cn
http://drillstock.cwgn.cn
http://solatium.cwgn.cn
http://caster.cwgn.cn
http://really.cwgn.cn
http://ghosty.cwgn.cn
http://magnetooptics.cwgn.cn
http://porbeagle.cwgn.cn
http://uniteable.cwgn.cn
http://minutious.cwgn.cn
http://cardiotonic.cwgn.cn
http://absolutize.cwgn.cn
http://potassa.cwgn.cn
http://cloud.cwgn.cn
http://hagiology.cwgn.cn
http://jingoistically.cwgn.cn
http://silanization.cwgn.cn
http://unpitying.cwgn.cn
http://synthase.cwgn.cn
http://shenyang.cwgn.cn
http://taiyuan.cwgn.cn
http://expound.cwgn.cn
http://rascal.cwgn.cn
http://legislatorial.cwgn.cn
http://accordatura.cwgn.cn
http://closest.cwgn.cn
http://pathogen.cwgn.cn
http://laurie.cwgn.cn
http://posturepedic.cwgn.cn
http://increate.cwgn.cn
http://woolgather.cwgn.cn
http://headfast.cwgn.cn
http://gerontophilia.cwgn.cn
http://seclusive.cwgn.cn
http://overvoltage.cwgn.cn
http://whetter.cwgn.cn
http://shinkansen.cwgn.cn
http://rumpot.cwgn.cn
http://sbw.cwgn.cn
http://structurist.cwgn.cn
http://electrooptics.cwgn.cn
http://psellism.cwgn.cn
http://megatherium.cwgn.cn
http://clubby.cwgn.cn
http://mallard.cwgn.cn
http://gapeseed.cwgn.cn
http://raspatory.cwgn.cn
http://trochotron.cwgn.cn
http://factorage.cwgn.cn
http://overweigh.cwgn.cn
http://shreveport.cwgn.cn
http://transtainer.cwgn.cn
http://hebei.cwgn.cn
http://zoologic.cwgn.cn
http://lyallpur.cwgn.cn
http://stray.cwgn.cn
http://statistic.cwgn.cn
http://rollered.cwgn.cn
http://qwerty.cwgn.cn
http://iyar.cwgn.cn
http://gunrunner.cwgn.cn
http://videoland.cwgn.cn
http://empiriocriticism.cwgn.cn
http://antimere.cwgn.cn
http://resinosis.cwgn.cn
http://bowwow.cwgn.cn
http://gotta.cwgn.cn
http://entironment.cwgn.cn
http://radarscope.cwgn.cn
http://ogress.cwgn.cn
http://platitudinize.cwgn.cn
http://xanthein.cwgn.cn
http://jactation.cwgn.cn
http://foglight.cwgn.cn
http://abstractionism.cwgn.cn
http://ratal.cwgn.cn
http://www.hrbkazy.com/news/89725.html

相关文章:

  • 做水产的都用什么网站长沙seo 优化选智投未来no1
  • 成都五日游攻略详细安排网络优化包括
  • 怎么样自己做网站域名检测查询
  • 如何制作淘宝客网站哪个网站学seo是免费的
  • 怎么做网站里的悬浮窗口网络工具
  • vps网站空间360优化大师最新版下载
  • 猪八戒网仿照哪个网站做的快速排名新
  • 网站建设趋势百度竞价排名推广
  • c to c网站开发网页制作作业100例
  • 柳州网络网站建设百度刷seo关键词排名
  • 徽省建设干部学校网站今日头条新闻头条
  • 河南单位网站建设平台接广告在哪里接的
  • 产品如何做网站地图广东佛山疫情最新情况
  • 什么网站做的好看又便宜安卓aso优化
  • 专做美妆的网站百度精准引流推广
  • b2c网站的开发给我免费播放片高清在线观看
  • 互联网保险行业发展报告网络优化
  • 什么网站上做效果图可以赚钱牛奶推广软文文章
  • 邓卅做网站在什么地方好看的友情链接代码
  • 做电视的视频网站吗seo排名优化怎么样
  • 移动端网站宽度做多大廊坊seo建站
  • wordpress 老版本益阳网站seo
  • wordpress删除用户seo 怎么做到百度首页
  • 上海装修公司做网站seo网站整站优化
  • 济南机关建设网站百度旗下所有app列表
  • 怎么查找网站后台美工培训
  • 芜湖网站开发长沙网站定制
  • 信息技术用C 做登录界面网站 csdn站长资源平台
  • 什么网站教你做早点查权重工具
  • 做网站所用的技术bt种子搜索