宝和网站建设/seo产品是什么意思
vue2状态管理Vuex
Vuex 是一个专为 Vue.js应用程序开发的状态管理模式。它使用集中式存储管理应用的所有组件的状态,以及规则保证状态只能按照规定的方式进行修改。
State(状态)
:Vuex 使用单一状态树,即一个对象包含全部的应用层级状态。这个状态树对应着一个应用中的所有状态。Getters(获取器)
:Getters 允许你在模板中计算状态。相当于组件中的计算属性。可以对 state 中的数据进行处理和过滤。Mutations(变更)
:Mutations 是 Vuex 修改状态的唯一方式,它们是同步事务。每个 mutation 都有一个字符串类型的事件类型 (type) 和 一个回调函数,该回调函数接受 state 作为其第一个参数。Actions(动作)
:Actions 类似于 Mutations,不同之处在于它们是异步的。Actions 提交 Mutations 来修改状态。Actions 可以包含任意异步操作。modules(模块)
:Vuex 允许将 store 分割成模块,每个模块都有自己的 state、mutations、actions、getters。
辅助函数:便于在组件中使用 Vuex 的功能
mapState
: 将 store 中的 state 映射为组件的计算属性。mapGetters
: 将 store 中的 getters 映射为组件的计算属性。mapMutations
: 将 store 中的 mutations 映射为组件的方法。mapActions
: 将 store 中的 actions 映射为组件的方法。
1、创建vue2项目
安装脚手架:
npm install -g @vue/cli
创建vue2项目:vue create vue2_myapp
(输入完命令后选择vue2)
2、安装Vuex依赖
在项目中使用npm或者yarn安装Vuex。
npm install vuex
或yarn add vuex
或npm install vuex@^3.5.0 --save
(指定版本使用)
3、在src目录下创建store目录,在下面创建js用于存储Vuex(命名通常为index.js或者store.js)
// store.js
import Vue from 'vue';
import Vuex from 'vuex';
import modulesA from './modules/modulesA'Vue.use(Vuex);export default new Vuex.Store({state: {//存储公共数据count: 100,}, mutations: {// 定义修改state数据的方法increment(state) {state.count++;},decrement(state) {state.count--;},},actions: {// 使用异步的方式来触发mutations中的方法进行提交},getters: {// 获取状态的方法getCount: (state) => state.count,},modules: {// 注册拆分的模块a:{//namespaced: true,//可以直接在modulesA配置在中...modulesA}}
});
当项目比较复杂时,可以在src/store下增加module,将数据拆成模块,下面举一个moduleA示例
// moduleA.js
import Vue from 'vue';
import Vuex from 'vuex';Vue.use(Vuex);//注意此处是导出一个模块不是new一个新vuex
export default{namespaced: true,state: {//存储公共数据countA: 200,}, mutations: {// 定义修改state数据的方法incrementA(state) {state.countA++;},decrementA(state) {state.countA--;},},actions: {// 使用异步的方式来触发mutations中的方法进行提交incrementAsyncA({ commit }) {// 模拟一个异步操作,例如从 API 获取数据setTimeout(() => {commit('incrementA');}, 1000);},},getters: {// 获取状态的方法getCountA: (state) => state.countA,},
};
4、在main.js中引入store
import Vue from 'vue'
import App from './App.vue'
import store from './store/store';//引入store Vue.config.productionTip = falsenew Vue({render: h => h(App),store,//注册store
}).$mount('#app')
5、在组件中使用
<template><div><h2>Root Module</h2><p>Count from Root Module: <!-- 显示方式一:通过计算属性获取getter -->{{ rootCount }} || <!-- 显示方式二:直接获取state中的值 -->{{ $store.state.count }} || <!-- 显示方式三:通过...mapState(['count'])直接使用count -->{{ count }}</p><button @click="incrementRoot">增加模块 A 计数</button><button @click="decrementRoot">减少模块 A 计数</button><h2>Module A</h2><p>Count from Module A: <!-- 显示方式一:通过计算属性获取getter,需要配置namespaced -->{{ moduleACount }} || <!-- 显示方式二:通过在store中注册的模块直接使用modulesA的值 -->{{ $store.state.a.countA }}</p><button @click="incrementModuleA">增加模块 A 计数</button><button @click="decrementModuleA">减少模块 A 计数</button><button @click="incrementModuleAAsync">异步增加模块 A 计数</button></div>
</template><script>
import { mapState,mapMutations,mapActions } from 'vuex';export default {computed: {// 1、使用mapState方式获取数据// mapState使用方式一...mapState(['count']),// mapState使用方式二...mapState({rootCountMapState: 'count', // 将根模块的 'getCount' 映射为 'rootCount'moduleACount: 'a/getCountA', // 将模块 'a' 的 'getCountA' 映射为 'moduleACount'}),// 2、使用 mapGetters 辅助函数将模块中的 getters 映射到组件的计算属性 rootCount() {return this.$store.getters.getCount;},moduleACount() {return this.$store.getters['a/getCountA'];},},methods: {// 1、使用mapMutations获取mutations模块方式一...mapMutations({incrementRoot: 'increment', // 将根模块的 'increment' 映射为 'incrementRoot'decrementRoot: 'decrement', // 将根模块的 'decrement' 映射为 'decrementRoot'incrementModuleA: 'a/incrementA', // 将模块 'a' 的 'incrementA' 映射为 'incrementModuleA'decrementModuleA: 'a/decrementA', // 将模块 'a' 的 'decrementA' 映射为 'decrementModuleA'}),...mapActions({incrementModuleAAsync: 'a/incrementAsyncA', // 将 'a/incrementAsyncA' 映射为 'incrementModuleAAsync'}),// 使用 mapMutations 辅助函数将模块中的 mutations 映射到组件的方法二incrementRoot() {this.$store.commit('increment');},decrementRoot() {this.$store.commit('decrement');},incrementModuleA() {this.$store.commit('a/incrementA');},decrementModuleA() {this.$store.commit('a/decrementA');},},
};
</script>
示例效果图:
vue3状态管理Pinia
State(状态)
: 在 Store 中定义的数据,即应用程序的状态。状态可以是基本类型、对象、数组等。Actions(操作)
: 在 Store 中定义的用于操作状态的函数。Actions 可以是同步或异步的,通过 this,你可以直接修改状态。如果 actions 返回一个 Promise,Pinia 将等待 Promise 完成,然后再继续执行其他代码。Getter(获取器)
:获取器允许你从状态中派生出一些衍生数据,类似于计算属性。通过 getters,你可以在不直接修改状态的情况下获取和处理状态的数据。
1、创建项目(参见上面vue2,输入命令后选择vue3即可)
2、安装pinia
npm install pinia
或yarn add pinia
3、在main.js中引入
import { createApp } from 'vue'
import App from './App.vue'
import { createPinia } from 'pinia';const app = createApp(App);app.use(createPinia());
app.mount('#app')
4、在src下创建store/index.js
import { defineStore } from 'pinia';export const useExampleStore = defineStore('example', {state: () => ({counter: 100,}),actions: {increment() { this.counter++;console.log(this.counter);},decrement() {this.counter--;},},
});
5、在组件中通过setup()使用,下面列举了五种改变state数据的方式。
<!-- src/components/ExampleComponent.vue -->
<template><div><p>Counter: {{ exampleStore.counter }}</p><button @click="increment">Increment</button><button @click="decrement">Decrement</button></div>
</template><script setup>
import { useExampleStore } from '../store/index';// 组合式
const exampleStore = useExampleStore();const increment=()=>{// 方式一:在store的action中操作exampleStore.increment();// 方式二:直接在应用的组件中改变// exampleStore.counter++;// 方式三:使用$patch整体覆盖// exampleStore.$patch({// counter:105// })// 方式四:使用$patch改变// exampleStore.$patch((state)=>{// if(state.counter){// state.counter++;// }// })// 方式五:使用$state覆盖// exampleStore.$state ={// counter:122// }
}
const decrement=()=>{// exampleStore.decrement();exampleStore.counter--;
}// 选项式
// export default {
// setup() {
// const exampleStore = useExampleStore();
// return {
// exampleStore,
// increment: exampleStore.increment,
// decrement: exampleStore.decrement,
// };
// }
// };
</script>
示例效果图:
Pinia与VueX区别:
- Vuex 为vue2打造的,Pinia为vue3打造的
- Pinia没有mutations,直接通过actions进行同步和异步操作,异步需要返回一个promise
- Pinia 没有modules,设计更为分散,每个组件可以拥有自己的 Store。
- Vuex 组件通过 mapState、mapMutations、mapActions 等辅助函数来访问存储库中的数据和操作。pinia通过setup 函数来引入和使用 Store,并通过 Store 的实例访问状态和操作。
- Vuex 对 TypeScript 有良好的支持,但类型推断可能有时候会感觉有点繁琐。Pinia 是使用 TypeScript 编写的,提供了更好的 TypeScript 支持,可以更轻松地推断和利用类型信息。
- vuex做数据持久化使用插件
vuex-persistedstate
,Pinia做数据持久化使用pinia-plugin-persist
react状态管理Redux
Provider
:把父组件传递进来的store对象放入react 上下文中,这样connect组件就可以从上下文中获取到store对象combineReducer
:store.state进行分片管理,每个reducer管理state中的一部分。由于createStore只接受一个reducer,所以采用该方法生成一个最终的reducer中间件(Middleware)
:中间件是一个位于动作派发和 Reducer 之间的拦截层。它允许你在动作被派发到 Reducer 之前执行额外的逻辑。State
:整个 Redux 应用程序的状态,它是只读的。状态的更新是通过触发动作来创建新的状态,而不是直接修改原有状态。action
:更新state的状态时用。dispatch
:触发store修改state的命令,是createStore返回对象的一个方法connect
:从react上下文中取出store对象,订阅store.state的变化,当store state变化时调用自身的方法重新生成connect组件的state,被包装组件便会被重新渲染。不会感知到store的存在,dispatch在这里也是非必须的。
connect的4个内置组件: 状态mapStateToProps
、动作mapDispatchToProps
、属性合并mergeProps
和 配置项options
异步中间件
:redux没有直接提供执行异步操作的方法,需要手动集成中间件,最常用异步实现的中间件有redux-thunk
、redux-saga
1、搭建react+ts项目
npm install -g create-react-app
npx create-react-app my-react-ts-app --template typescript
2、安装redux
npm install redux react-redux
3、在src下创建如下结构
src/
– – store/
– – – – index.ts
– – – – reducers/
– – – – – – index.ts
– – – – – – counterReducer.ts
// src/store/index.ts
import { configureStore } from '@reduxjs/toolkit';
import rootReducer from './reducers';const store = configureStore({reducer: rootReducer
});export default store;
// src/store/reducers/index.ts
// combineReducers 用于将多个 reducer 组合成一个根 reducer
import { combineReducers } from 'redux';
import counterReducer from './counterReducer';const rootReducer = combineReducers({counter: counterReducer,// 添加其他 reducer...
});export default rootReducer;
// src/store/reducers/counterReducer.ts// 从Redux中导入Action类型,用于定义动作对象
import { Action } from 'redux';// 定义动作类型的常量,以避免拼写错误
const INCREMENT = 'INCREMENT';
const DECREMENT = 'DECREMENT';// Action Creators: 返回动作对象的函数
export const increment = (): Action => ({ type: INCREMENT });
export const decrement = (): Action => ({ type: DECREMENT });// Reducer函数:根据派发的动作更新状态
const counterReducer = (state = 0, action: Action): number => {switch (action.type) {case INCREMENT:// 当派发INCREMENT动作时,将当前状态加1return state + 1;case DECREMENT:// 当派发DECREMENT动作时,将当前状态减1return state - 1;case 'INCREMENTFIXED':return state + 5;default:// 如果动作类型不被识别,则返回当前状态return state;}
};// 将counterReducer作为模块的默认导出
export default counterReducer;
4、在index.tsx中引入
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
//引入redux
import { Provider } from 'react-redux';
import store from './store';const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement
);
root.render(<React.StrictMode><Provider store={store}><App /></Provider></React.StrictMode>);reportWebVitals();
5、在components中创建Counter.tsx
// src/components/Counter.tsx
import React from 'react';
import { connect } from 'react-redux';
import { increment, decrement } from '../store/reducers/counterReducer';interface CounterProps {count: number;increment: () => void;decrement: () => void;
}const Counter: React.FC<CounterProps> = ({ count, increment, decrement }) => {return (<div><p>Count: {count}</p><button onClick={increment}>Increment</button><button onClick={decrement}>Decrement</button></div>);
};// 中间件mapStateToProps 函数将 Redux store 的状态映射到组件的属性。
const mapStateToProps = (state: { counter: number }) => ({count: state.counter,
});// 中间件mapDispatchToProps 对象将动作创建函数映射到组件的属性。
const mapDispatchToProps = {increment,decrement
};// connect 函数将组件连接到 Redux store,并将 mapStateToProps 和 mapDispatchToProps 的结果传递给组件
export default connect(mapStateToProps, mapDispatchToProps)(Counter);
示例效果图:
相关文章:

vue2(Vuex)、vue3(Pinia)、react(Redux)状态管理
vue2状态管理Vuex Vuex 是一个专为 Vue.js应用程序开发的状态管理模式。它使用集中式存储管理应用的所有组件的状态,以及规则保证状态只能按照规定的方式进行修改。 State(状态):Vuex 使用单一状态树,即一个对象包含全部的应用层…...

用户画像项目背景
1,用户画像项目介绍 大数据平台简介 数据仓库+用户画像+推荐系统 (1)数据仓库:加快数据的分析和查询 数据仓库分层:ODS层(映射HDFS的数据)—DW(数据仓库层)–APP(层)—BI(层) DW:DWD明细数据层(数据的清洗和转换),DWM(轻度聚合层),DWS(高度聚合),APP(层),DIM(层) …...

Go使用记忆化搜索的套路【以20240121力扣每日一题为例】
题目 分析 这道题很明显记忆化搜索,用py很容易写出来 Python class Solution:def splitArray(self, nums: List[int], k: int) -> int:n len(nums)# 寻找分割子数组中和的最小的最大值s [0]for num in nums:s.append(s[-1] num)#print(s)cachedef dfs(cur,…...

【LeetCode】每日一题 2024_1_21 分割数组的最大值(二分)
文章目录 LeetCode?启动!!!题目:分割数组的最大值题目描述代码与解题思路 LeetCode?启动!!! 今天是 hard,难受,还好有题解大哥的清晰讲解 题目&a…...

bevy the book 20140118翻译(全)
源自:Bevy Book: Introduction 主要用 有道 翻译。 Introduction 介绍 Getting Started 开始 Setup 设置 Apps 应用程序 ECS Plugins 插件 Resources 资源 Next Steps 下一个步骤 Contributing 贡献 Code 代码 Docs 文档 Building Bevys Ecosystem 构建 b…...

MySQL数据库面试知识点
1、数据库基础: MySQL是一个开源的关系型数据库管理系统,用于存储、管理和检索数据。它支持多种存储引擎,包括InnoDB、MyISAM等。MySQL是由瑞典公司MySQL AB开发,后来被Sun Microsystems收购,最终被甲骨文公司(Oracle…...

超优秀的三维模型轻量化、格式转换、可视化部署平台!
1、基于 HTML5 和 WebGL 技术,可在主流浏览器上进行快速浏览和调试,支持PC端和移动端 2、自主研发 AMRT 展示框架和9大核心技术,支持3D模型全网多端流畅展示与交互 3、提供格式转换、减面展UV、烘焙等多项单模型和倾斜摄影模型轻量化服务 4、…...

云原生全栈监控解决方案(全面详解)
【作者】JasonXu 前言 当前全球企业云化、数字化进程持续加速,容器、微服务等云原生技术在软件架构中快速渗透,IT 架构云化、复杂化持续驱动性能监控市场。企业云化、数字化持续转型,以及为了考虑系统的弹性、效率,企业软件开发中…...

代码随想录二刷 | 回溯 |复原IP地址
代码随想录二刷 | 回溯 |复原IP地址 题目描述解题思路代码实现 题目描述 93.复原IP地址 给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。 有效的 IP 地址 正好由四个整数(每个整数位于 0 到 255 之间组成&am…...

windows资源管理器占用过高CPU的问题
最近,笔者的电脑在进行文件操作时变得异常的卡顿,打开任务管理器发现windows资源管理器占用了50%-80%的CPU。这里指的文件操作包括但不限于解压,复制,粘贴,甚至重命名一个文件夹都会引起50%的CPU占用。起初笔者认为可能…...

redis的常见数据类型和应用场景(非八股)------大总结(学了要会用-------教你如何使用)
Redis的数据类型 Redis 提供了丰富的数据类型,常见的有五种: String(字符串),Hash(哈希),List(列表),Set(集合)、Zset&am…...

UE 可靠UDP实现原理
发送 我们的消息发送都是通过 UChannel 来处理的,通过调用 UChannel::SendBunch 统一处理。 发送的 Bunch 是以 FOutBunch 的形式存在的。当 bReliable 为 True 的时候,表示 Bunch 是可靠的。 发送逻辑直接从UChannel::SendBunch处开始分析 1、大小限…...

智慧博物馆信息化系统建设(1)
博物馆RFID藏品管理系统 博物馆藏品保管是一项十分复杂又繁琐的工作。从事保管工作除了经常、及时地进行藏品的登记、分类、编目、保养和修复等一系列工作外,还需要把有关藏品的信息迅速、正确地提供给利用者。要提高保管工作的效率,达到现代化的科学管理,从发展趋势看,进…...

【数据结构和算法】--- 二叉树(3)--二叉树链式结构的实现(1)
目录 一、二叉树的创建(伪)二、二叉树的遍历2.1 前序遍历2.2 中序遍历2.3 后序遍历 三、二叉树节点个数及高度3.1 二叉树节点个数3.2 二叉树叶子节点个数3.3二叉树第k层节点个数3.4 二叉树查找值为x的节点 四、二叉树的创建(真) 一、二叉树的创建(伪) 在学习二叉树的基本操作前…...

Cesium for Unity包无法加载
太上老君急急如律⚡令⚡ 🥙关闭UnityHub🧀启动梯子🥪cmd 启动UnityHub 🥙关闭UnityHub 🧀启动梯子 🥪cmd 启动UnityHub 把批处理启动文件👈中的exe的路径换成自己的安装目录!保存…...

Leetcode—40.组合总和II【中等】
2023每日刷题(七十七) Leetcode—40.组合总和II 算法思想 实现代码 class Solution { public:vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {vector<vector<int>> ans;vector<int…...

vscode连不上虚拟机,一直密码错误
最近在做毕设,但是vscode使用连接不上虚拟机,我以为是网络配置的问题,一顿查阅没找到原因。 后来查了一下ssh的日志,发现ssh有消息,但是也提示密码错误。 没找到密码配置格式什么的,经查看sshd配置文件发现…...

力扣每日一题 --- 972. 相等的有理数
本题中的一个难点是怎么判断是否相等,如果自己写判断的话是不是很麻烦,判断整数之后再去判断小数部分,那么我们这题的另一个难点就要登场了,第一个难点让本题的情况变得复杂,第二个难点让本题变得很难想到怎么判断&…...

EXECL 单元格字符串链接 CONCAT :应用:将一行数据转为json
源: 目标 函数表示 CONCAT("data", CHAR(10), "{", CHAR(10), " ", "ulAlarmId : ", A5, CHAR(10), " ", "ulAlarmLevel : ", D5, CHAR(10)," ", "bBo…...

基于Python实现人脸识别相似度对比
目录 引言背景介绍目的和意义 人脸识别的原理人脸图像获取人脸检测与定位人脸特征提取相似度计算 基于Python的人脸相似度对比实现数据集准备人脸图像预处理特征提取相似度计算 引言 背景介绍 人脸识别技术是一种通过计算机对人脸图像进行分析和处理,从而实现自动识…...

CSS 蜡烛效果
<template><view class="holder"><!-- 身子 --><view class="candle"><!-- 光源 --><view class="blinking-glow"></view><!-- 火星子 --><view class="thread"></view>…...

渗透测试之Kali如何利用CVE-2019-0708漏洞渗透Win7
环境: 1.攻击者IP:192.168.1.10 系统: KALI2022(vmware 16.0) 2.靶机IP:192.168.1.8 系统:Windows 7 6.1.7601 Service Pack 1 Build 7601 已开启远程协助RDP服务开启了3389端口 问题描述: KALI 如何利用CVE-2019-0708漏洞渗透Win7 解决方案: 1.打开kali,msf搜索…...

Docker(二)安装指南:主要介绍在 Linux 、Windows 10 和 macOS 上的安装
作者主页: 正函数的个人主页 文章收录专栏: Docker 欢迎大家点赞 👍 收藏 ⭐ 加关注哦! 安装 Docker Docker 分为 stable test 和 nightly 三个更新频道。 官方网站上有各种环境下的 安装指南,这里主要介绍 Docker 在…...

LeetCode 410. 分割数组的最大值
一、题目 1、题目描述 给定一个非负整数数组 nums 和一个整数 k ,你需要将这个数组分成 k 个非空的连续子数组。 设计一个算法使得这 k 个子数组各自和的最大值最小。 2、接口描述 class Solution { public:int splitArray(vector<int>& nums, int …...

linux shell脚本 基础认识
Linux 系统中的 Shell 是一个特殊的应用程序,它介于操作系统内核与用户之间,充当 了一个“命令解释器”的角色,负责接收用户输入的操作指令(命令)并进行解释,将需要执行的操作传递给内核执行,并…...

一文(10图)了解Cornerstone3D核心概念(万字总结附导图)
Cornerstone3D介绍 Cornerstone3D是一个专门为处理三维医学影像而设计的JavaScript库。 它是Cornerstone项目的一部分,旨在为医学影像社区提供高性能、可扩展且易于使用的开源Web工具,专注于提供交互式的3D医学图像浏览体验,适用于多种医学…...

牛客网-----跳石头
题目描述: 一年一度的“跳石头”比赛又要开始了! 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石。组委会已经选择好了两块岩石作为比赛起点和终点。在起点和终点之间,有N块岩石(不含起点和终点的岩石)。在比赛过程中࿰…...

用ChatGPT教学、科研!大学与OpenAI合作
亚利桑那州立大学(简称“ASU”)在官网宣布与OpenAI达成技术合作。从2024年2月份开始,为所有学生提供ChatGPT企业版访问权限,主要用于学习、课程作业和学术研究等。 为了帮助学生更好地学习ChatGPT和大语言模型产品,AS…...

运维平台介绍:视频智能运维平台的视频质量诊断分析和告警中心
目 录 一、视频智能运维平台介绍 (一)平台概述 (二)结构图 (三)功能介绍 1、运维监控 2、视频诊断 3、巡检管理 4、告警管理 5、资产管理 6、工单管理 7、运维…...

GAMES104-现代游戏引擎:从入门到实践 - 物理引擎课程笔记汇总
文章目录 0 入门资料1 物理引擎基本概念Actor & shapesRigid body dynamicsCollision DetectionCollision Resolution 应用与实践Character controllerRagdoll 0 入门资料 GAMES104-现代游戏引擎:从入门到实践_课程视频_bilibiliGAMES104官方账号 - 知乎课程主页…...