Vuex理解
创新互联公司 长期为成百上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为冠县企业提供专业的做网站、
成都网站制作 ,
冠县网站改版 等技术服务。拥有十多年丰富建站经验和众多成功案例,为您定制开发。
vuex 是一个专门为vue.js设计的集中式状态 管理架构。状态?把它理解为在data中的属性需要共享给其他vue组件使用的部分,就叫做状态。简单的说就是data中需要共用的属性 。比如:有几个页面要显示用户名称和用户等级,或者显示用户的地理位置。如果不把这些属性设置为状态,那每个页面遇到后,都会到服务器进行查找计算,返回后再显示。在中大型项目中会有很多共用的数据,所以尤大神给我们提供了vuex。
引入vuex 1.利用npm包管理工具,进行安装 vuex。在控制命令行中输入下边的命令就可以了。
需要注意的是这里一定要加上 –save,因为你这个包在生产环境中是要使用的。
2.新建一个vuex文件夹(这个不是必须的),并在文件夹下新建store.js文件,文件中引入我们的vue和vuex。
1
2
import Vue from 'vue';
import Vuex from 'vuex';
3.使用vuex,引入之后用Vue.use进行引用。
实例
1.现在我们store.js文件里增加一个常量对象。store.js文件就是我们在引入vuex时的那个文件。
1
2
3
const state={
count:1
}
2.用export default 封装代码,让外部可以引用。
1
2
3
4
export default new Vuex.Store({
state
})
3.新建一个vue的模板,位置在components文件夹下,名字叫count.vue。在模板中我们引入我们刚建的store.js文件,并在模板中用{{$store.state.count}}输出count 的值。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{{msg}}
{{$store.state.count}}
import store from '@/vuex/store'
export default {
data(){
return {
msg:'Hello Vuex',
}
},
store
}
4.在store.js文件中加入两个改变state的方法。
1
2
3
4
5
6
7
8
const mutations={
add(state){
state.count++;
},
reduce(state){
state.count--;
}
}
这里的mutations是固定的写法,意思是改变的,只知道要改变state的数值的方法,必须写在mutations里就可以了。
5.在count.vue模板中加入两个按钮,并调用mutations中的方法。
1
2
3
4
@click="$store.commit('add')"> +
@click="$store.commit('reduce')"> -
State访问状态对象
一、通过computed的计算属性直接赋值
computed属性可以在输出前,对data中的值进行改变,利用这种特性把store.js中的state值赋值给模板中的data值。
1
2
3
4
5
computed:{
count(){
return this .$store.state.count;
}
}
这里需要注意的是return this.$store.state.count这一句,一定要写this,要不会找不到$store的。这种写法很好理解,但是写起来是比较麻烦的。
二、通过mapState的对象来赋值 首先要用import引入mapState。
1
import {mapState} from 'vuex';
然后还在computed计算属性里写如下代码:
1
2
3
computed:mapState({
count:state=>state.count
})
这里使用ES6的箭头函数来给count赋值。
三、通过mapState的数组来赋值 1
computed:mapState(["count"])
Mutations修改状态
$store.commit( ) Vuex提供了commit方法来修改状态
1
2
@click="$store.commit('add')"> +
@click="$store.commit('reduce')"> -
store.js文件:
1
2
3
4
5
6
7
8
const mutations={
add(state){
state.count++;
},
reduce(state){
state.count--;
}
}
传值: 这只是一个最简单的修改状态的操作,在实际项目中常常需要在修改状态时传值。比如上边的例子,是每次只加1,而现在要通过所传的值进行相加。其实我们只需要在Mutations里再加上一个参数,并在commit的时候传递就就可以了。来看具体代码: 现在store.js文件里给add方法加上一个参数n。添加的地方已经标黄了。
1
2
3
4
5
6
7
8
const mutations={
add(state,n){
state.count+=n;
},
reduce(state){
state.count--;
}
}
在Count.vue里修改按钮的commit( )方法传递的参数,我们传递10,意思就是每次加10.
1
2
3
4
@click="$store.commit('add',10)"> +
@click="$store.commit('reduce')"> -
这样两个简单的修改就完成了传值,可以在浏览器中实验一下了。
模板获取Mutations方法 实际开发中也不喜欢看到$store.commit( )这样的方法出现,希望跟调用模板里的方法一样调用。
例如:@click=”reduce” 就和没引用vuex插件一样。
要达到这种写法,只需要简单的两部就可以了:
1在模板count.vue里用import 引入我们的mapMutations:
1
import { mapState,mapMutations } from 'vuex';
2在模板的
基本
文件
流程
错误
SQL
调试
请求信息 : 2026-05-24 13:48:44 HTTP/1.1 GET : /article/cedjgs.html 运行时间 : 1.7046s ( Load:0.0067s Init:1.0121s Exec:0.6732s Template:0.0126s ) 吞吐率 : 0.59req/s 内存开销 : 2,270.28 kb 查询信息 : 12 queries 5 writes 文件加载 : 36 缓存信息 : 0 gets 0 writes 配置加载 : 130 会话信息 : SESSION_ID=i7j8oo3lsndg4dkv35i910hli6
/www/wwwroot/tsicrk.com/index.php ( 1.09 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/ThinkPHP.php ( 4.61 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Think.class.php ( 12.26 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Storage.class.php ( 1.37 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Storage/Driver/File.class.php ( 3.52 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Mode/common.php ( 2.82 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Common/functions.php ( 53.56 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Hook.class.php ( 4.01 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/App.class.php ( 13.49 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Dispatcher.class.php ( 14.79 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Route.class.php ( 13.36 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Controller.class.php ( 11.23 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/View.class.php ( 7.59 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/BuildLiteBehavior.class.php ( 3.68 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ParseTemplateBehavior.class.php ( 3.88 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php ( 1.91 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Conf/convention.php ( 11.15 KB ) /www/wwwroot/tsicrk.com/App/Common/Conf/config.php ( 2.14 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Lang/zh-cn.php ( 2.55 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Conf/debug.php ( 1.49 KB ) /www/wwwroot/tsicrk.com/App/Home/Conf/config.php ( 0.31 KB ) /www/wwwroot/tsicrk.com/App/Home/Common/function.php ( 3.33 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.class.php ( 5.62 KB ) /www/wwwroot/tsicrk.com/App/Home/Controller/ArticleController.class.php ( 6.02 KB ) /www/wwwroot/tsicrk.com/App/Home/Controller/CommController.class.php ( 1.60 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Model.class.php ( 60.11 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php ( 32.43 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db/Driver/Pdo.class.php ( 16.74 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Cache.class.php ( 3.83 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Cache/Driver/File.class.php ( 5.87 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template.class.php ( 28.16 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template/TagLib/Cx.class.php ( 22.40 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template/TagLib.class.php ( 9.16 KB ) /www/wwwroot/tsicrk.com/App/Runtime/Cache/Home/7540f392f42b28b481b30614275e4e55.php ( 17.71 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.class.php ( 0.97 KB ) /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ShowPageTraceBehavior.class.php ( 5.24 KB )
[ app_init ] --START-- Run Behavior\BuildLiteBehavior [ RunTime:0.000007s ] [ app_init ] --END-- [ RunTime:0.000040s ] [ app_begin ] --START-- Run Behavior\ReadHtmlCacheBehavior [ RunTime:0.000292s ] [ app_begin ] --END-- [ RunTime:0.000315s ] [ view_parse ] --START-- [ template_filter ] --START-- Run Behavior\ContentReplaceBehavior [ RunTime:0.000072s ] [ template_filter ] --END-- [ RunTime:0.000102s ] Run Behavior\ParseTemplateBehavior [ RunTime:0.009224s ] [ view_parse ] --END-- [ RunTime:0.009262s ] [ view_filter ] --START-- Run Behavior\WriteHtmlCacheBehavior [ RunTime:0.000202s ] [ view_filter ] --END-- [ RunTime:0.000224s ] [ app_end ] --START--
1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`pid`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( pid= ) [8] Undefined index: pid /www/wwwroot/tsicrk.com/App/Home/Controller/ArticleController.class.php 第 47 行. [8] Undefined index: db_host /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 120 行. [8] Undefined index: db_port /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 121 行. [8] Undefined index: db_name /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 122 行.
1.7046s