vue爬坑记:初学vuex(一)

前端|Vue Vuex和Pinia Vue状态管理的双雄较量 Vuex和Pinia的区别 Vuex和Pinia都是Vue生态中非常重要的状态管理解决方案。Vuex凭借其官方背书和历史悠久的社区支持,适合需要复杂状态管理的大型项目。而Pinia则以其现代化的设计和开发体验吸引了许多Vue开发者,特别是对于新项目或寻求更简单状态管理方法的开发者来说。项目规模和复杂性团队对TypeScript的偏好现有项目是否已经使用了Vuex对状态管理的个人偏好无论选择哪一个,重要的是理解它们的核心概念和实现方式,以便能够有效地管理和维护应用程序的状态。 阅读详情
  • 安装vuex
npm install vuex --save

src目录下新建vuex文件夹,新建store.js

import Vue from 'vue'
import Vuex from 'vuex'


Vue.use(Vuex)


const state = {
    count: 5
}

const mutations = {
    add(state) {
        state.count++
    },
    reduce(state) {
        state.count--
    }
}
export default new Vuex.Store({
    state,mutations
})
  • components文件夹下新建count.vue文件
<template>
    <div>
        <h2>{{msg}}</h2>
        <hr />
        <h3>{{$store.state.count}}</h3> 
        <h3>{{count}}</h3> 
        <div>
            <button @click="$store.commit('add')">+</button>    
            <button @click="$store.commit('reduce')">-</button>    
        </div> 
    </div>
</template>
<script>
import store from "@/vuex/store";
import { mapState } from "vuex";
export default {
  data() {
    return {
      msg: "hello Vuex"
    };
  },
  computed: mapState(["count"]),
  //   computed:{
  //       count(){
  //           return this.$store.state.count;
  //       }
  //   },
  store
};
</script>

  • router文件夹下修改index.js
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Count from '@/components/count'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    },
    {
      path: '/count',
      name: 'Count',
      component: Count
    }
  ]
})

浏览器的地址栏后面输入count就可以直接访问

EMC工程师职业发展:从测试整改到系统设计的进阶之路 电磁兼容(EMC)是确保电子设备在复杂电磁环境中可靠工作的关键技术,其核心在于控制电磁干扰(EMI)并提升抗扰度。其基本原理涉及电磁场理论、噪声耦合路径与滤波屏蔽技术。对于电子产品而言,EMC设计直接关系到能否通过CE、FCC等强制性认证,是产品上市和市场竞争力的关键。在工程实践中,EMC工程师需要从电路设计、PCB布局、元器件选型等源头入手,将EMC要求融入产品开发全流程,实现从后期“整改”到前期“设计预防”的价值跃迁。本文聚焦EMC工程师的核心能力体系构建与职业发展路径,深入探讨了如何从掌握标准测试、电 阅读详情

相关推荐

未隔离电源设备使用usb转串口调试导致串口模块爆炸烧坏事故记录

这个事就发生在前两天,我们公司的wifi模块被客户用到了咖啡机上,客户寄了一台整机给我们协助调试,还送了几包咖啡豆,本想着还能打个咖啡喝一喝的,但就在咖啡机刚寄过来第一天,就出事了。而这个事故的发生,竟然仅仅是因为同事接了个串口模块到电脑调试打印导致的。谁也想不到,这个3.3V的供电的wifi模块居然还有这么大的威力。直接将usb转TTL串口模块的转换芯片给爆开了,咖啡机也给弄坏了,据同事描述当时情况就只见到一片火光和一声巨响,状况比较惨烈,所幸的是笔记本电脑没有烧坏,但是直接给崩蓝屏了。 ...

fangye945a的博客 6195

【vue运行报错】There are multiple modules with names... 报错原因和解决办法

There are multiple modules with names that only differ in casing.

我有满天星辰的博客 5835

用Java实现对接豆包ai火山引擎详细版简单明了ai对话

第二部分代码实现。

m0_72637700的博客 8612

There are multiple modules with names that only differ in casing... 报错原因和解决办法

There are multiple modules with names that only differ in casing. This can lead to unexpected behavior when compiling on a filesystem with other case-semantic. Use equal casing. Compare these module ...

轻风细雨_林木木 4472

gmsv源代码c语言,石器时代ABLua的原理、简介、过程、运用、收发

操作原理(一)服务端:服务器源文件客户端:客户所操作的程序(既是游戏客户端)接口:既是某项功能(例如:鼠标点击,文字发送,战斗,观战,人物动作等)返回的值ABlua:在源代码中添加的整套引擎作为回调接口--lua所实现的功能是基于服务端源码添加的接口由该接口与客户端发送封包实现功能源码里是否有ABlua回调接口?例如:源码里的gmsv/battle.c文件(既是战斗回调文件)以下为简单描述C给予l...

weixin_35223575的博客 2411

Fluent 中多相流模型全面解析

3.颗粒或液滴在连续相中的运动:若关注颗粒或液滴在连续相中的运动轨迹、分布规律以及与连续相的相互作用,如喷雾干燥、气力输送等过程,Eulerian-Lagrangian 模型则是最佳选择。Eulerian-Eulerian 模型可以准确模拟气体在固体颗粒中的流动情况,包括颗粒的流化状态、气体与颗粒之间的传热传质过程,为气固流化床的优化设计和操作提供依据。通过该模型,可以了解混合器内不同位置的混合程度、颗粒的运动轨迹以及液固相间的相互作用,从而改进混合器的结构和操作条件,提高混合效率。

khhdp的博客 3087

There are multiple modules with names that only differ in casing. This can lead to unexpected······

报错内容 There are multiple modules with names that only differ in casing. This can lead to unexpected behavior when compiling on a filesystem with other case-semantic. Use equal casing. Compare these module identifiers 翻译过来是: 有多个模块的名称仅在大小写上不同。 在使用其他大小写语义的文件系

qq_46193186的博客 2744

基于紫光展锐平台的音频调用到hal层逻辑

基于紫光展锐平台的音频调用到hal层逻辑 1、数据流程图 AudioPolicyService和AudioFlinger的创建: 这两个服务都在audioserver中启动,文件路径: frameworks/av/media/audioserver/main_audioserver.cpp 从AudioFlinger和AudioPolicyService的创建,到output的打开,以及AudioFlinger开始往底层写入数据的具体流程如下图: #mermaid-svg-kjHL4XWwkQXN6NdA

weixin_55053963的博客 1355

【已解决】There are multiple modules with names that only differ in casingThis can lead to unexpected错误解决

项目场景: 今天早上运行Vue项目的时候需要在一个组件里面引入两个组件的模板,这时报错。 问题描述: warning in ./src/components/SideBar.vue There are multiple modules with names that only differ in casing. This can lead to unexpected behavior when compiling on a filesystem with other case-semantic. U

海海不掉头发的的博客 1万+

Realsense D435i运行ORB-SLAM3

【代码】Realsense D435i运行ORB-SLAM3。

MAX_Hope的博客 2563

RH850从0搭建Autosar开发环境【3X】- Davinci Configurator之BswM模块配置详解(上)

RH850从0搭建Autosar开发环境【3X】- Davinci Configurator之BswM模块配置详解(上)

weixin_42412049的博客 740

vue项目编译时报 There are multiple modules with names that only differ in casing. This can lead to unexpec

今天拉取代码后,npm run dev时发现 会报warning There are multiple modules with names that only differ in casing. This can lead to unexpected behavior when compiling on a filesystem with other case-semantic. Use equal casing. Compare these module identifiers 大概意思就是 有多个模块

weixin_42190844的博客 1581

人形机器人 vs 机器狗SLAM:FAST_LIO_LOCALIZATION_HUMANOID的差异化优化方向

《FAST_LIO_LOCALIZATION_HUMANOID在人形与机器狗应用中的差异化优化策略》摘要: 格灵深瞳开源的FAST_LIO_LOCALIZATION_HUMANOID系统针对人形机器人和机器狗提出了差异化优化方案。人形机器人需应对双足步态带来的周期性晃动,通过动态盲区滤波、二阶运动模型补偿步态相位畸变,并修复重力向量计算误差。机器狗则需解决四足高频运动挑战,包括设计自适应地面分割算法、抑制IMU高频振动噪声,以及基于足端接触序列的非匀速运动建模。二者在传感器配置(高度/视角)、运动频率(1-

Studying…… 326

vue中使用vuex(超详细)

vuex是使用vue中必不可少的一部分,基于父子、兄弟组件,我们传值可能会很方便,但是如果是没有关联的组件之间要使用同一组数据,就显得很无能为力,那么vuex就很好的解决了我们这种问题,它相当于一个公共仓库,保存着所有组件都能共用的数据。 那么,我们一起来看看vue项目怎么使用它吧。(如果你对vuex有一定了解,不是刚接触的小白,请忽略第一步,直接查看第二步) 一、适合初学者使用,保存数据以及...

一只前端小菜鸟 18万+

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

Vuex详解,一文读懂并使用Vuex

如果您需要构建一个中大型单页应用,您很可能会考虑如何更好地在组件外部管理状态,Vuex 将会成为自然而然的选择。Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式 + 库。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。

万维网连五洲,二进制写尽天下事,喜欢结识新伙伴寻找志同道合的朋友,欢迎一起探讨学习 1万+

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

《Vue入门到精通系列之六》--- Vuex详解

Vue系列教程持续更新中 全套Vue资料等你来拿

白大锅的博客,毕设WangLoveBai_999 4万+

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

Vue Vuex详解,一文彻底搞懂Vuex

本文深入解析 Vuex 的核心概念与工作流程,包括 State、Getters、Mutations、Actions 和 Modules 五大核心要素,并提供代码示例。Vuex 通过集中式状态管理解决组件间复杂数据传递问题,采用单向数据流确保状态变更可预测,支持模块化以提升大型应用的可维护性。文章还对比了 Vuex 的优势,如调试友好性和响应式更新机制,帮助开发者全面掌握这一 Vue.js 生态中的关键状态管理工具。

likuoelie的博客 1210

vue2 vuex

vue2 vuex

小钟不想敲代码 4万+

Vue中使用vuex之state

在vue中使用vuex之state,下面是一个简单的教程,适合新手学习vuex的时候使用。 首先要引入一下vue和vuex,为了方便学习,我就直接在头部引进cdn <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"...

前端李小白的博客 1781

vuex状态管理初学demo,一看就会

vuex状态管理初学,一看就会 1. vuex安装: npm install vuex --save 2. 新建store.js store.js中代码如下 import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ //state理解为管理状态的容器,变量在这里面声明,像vue页面的data() state: { count:0 },methods

Dawnchen1的博客 255

Vuex初学一

理解Vuex什么是vuexvuex的入门使用功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入 什么是vuex 于vuex...

hjh18728801803的博客 161

记初学vue完成的第一个demo

7.5来到苏小研报道,7.10正式参加实习工作,布置的任务要求就是熟悉vue框架,然后按要求完成一个demo。从最初的慌,懵到此刻的静,定,这一个月的学习还是很有收获的,特记录一下。项目地址:https://github.com/happy8silence/PMO.git。项目运行:npm install npm run serve 这个项目的技术栈是vue+vuex+vue-router+a...

silenmm的博客 8341

VueX:初学者指南与深入理解

本文为初学者提供了一个关于如何使用 VueX 的指南,并帮助读者深入理解其核心概念。VueX 是 Vue.js 的官方状态管理库,用于集中管理应用状态,确保状态以可预测的方式变化。

x15514104477的博客 737

初学Vue(全家桶)-第16天(vuex):vuex简介

专门在Vue中实现集中式状态(数据)管理的一个插件,对Vue应用中多个组件的共享状态进行集中式管理(读/写),也是一种组件间通信的方式,且适用于任意组件间通信。(简单点说,vuex的作用就相当于一个存储室,将数据(状态)存在其中,当组件要用的时候,就从这里面拿;当一个组件修改里面的内容后,其他组件访问到的是修改后的内容)官方原理图 (1)安装:,需要注意的是vue2要用vuex3版本,vue3要用vuex4版本,要@符号指定版本 (2)在src下创建文件夹,取名store,其下在创建一个index.js文

lalala_dxf的博客 459

【Vue系列】vuex详解,一篇彻底搞懂vuex

本文全面解析Vuex状态管理库,针对Vue 2/3开发者提供从基础到进阶的实用指南。内容涵盖Vuex核心概念(State、Mutations、Actions等)、安装配置、严格模式、模块化开发以及Vue 3的Composition API集成。文章通过代码示例演示核心功能实现,对比Vuex与Pinia的适用场景,并给出大型项目的最佳实践方案。特别强调Vuex在中大型应用中的状态管理优势,同时指出其在小项目中的局限性。适合各阶段Vue开发者系统学习状态管理方案,为技术选型提供参考。

likuolei的博客 1255

Vuex2.0边学边记+两个小例子

转载自:http://www.jianshu.com/p/13bec8f5b17d 最近在研究Vuex2.0,搞了好几天终于有点头绪了。 首先vuex概念比较多,一定要搞懂里面的概念,可以参考官网Vuex2.0概念,我写此文的目的是希望能对前端爱好者提供个参考,加深对vuex2.0各核心概念的理解。 废话少说,直接上干货。这是官网上的一个计数的实例。 先来起个项

wulixiaoxiao1的博客 7331
上一篇: LESS使用方法小计
下一篇: vue爬坑记:关于遇到侧边栏高亮的坑
AlexKain
博客等级 码龄8年 35粉丝 105原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值