<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title></head><script src="./js/vuex.js"></script><script src="./js/vue2.0.js"></script><body> <div id="app"> </div></body><script> Vue.use(Vuex);//在建立Vue实例以前 var myStore = new Vuex.Store({ state:{ //存放组件之间共享的数据 name:"jjk" }, mutations:{ //显式的更改state里的数据 }, getters:{ //获取数据的方法 }, actions:{ // } }); new Vue({ el:"#app", data:{ name:"dk" }, store:myStore, mounted:function(){ console.log(this)//控制台 } }) </script></html>
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title></head><script src="./js/vuex.js"></script><script src="./js/vue2.0.js"></script><body> <div id="app"> <hello></hello> </div></body><script> Vue.use(Vuex); var myStore = new Vuex.Store({ state:{ //存放组件之间共享的数据 name:"jjk" }, mutations:{ //显式的更改state里的数据 }, getters:{ //过滤state数据 }, actions:{ // } }); Vue.component('hello',{ template:"<p>{{name}}</p>", computed: { name:function(){ return this.$store.state.name } }, mounted:function(){ console.log(this) } }) new Vue({ el:"#app", data:{ name:"dk" }, store:myStore, mounted:function(){ console.log(this) } }) </script></html>
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title></head><script src="./js/vuex.js"></script><script src="./js/vue2.0.js"></script><body> <div id="app"> <hello></hello> </div></body><script> Vue.use(Vuex); var myStore = new Vuex.Store({ state:{ //存放组件之间共享的数据 name:"jjk", age:18 }, mutations:{ //显式的更改state里的数据 }, getters:{ getAge:function(state){ return state.age; } }, actions:{ // } }); Vue.component('hello',{ template:"<p>姓名:{{name}} 年龄:{{age}}</p>", computed: { name:function(){ return this.$store.state.name }, age:function(){ return this.$store.getters.getAge } }, mounted:function(){ console.log(this) } }) new Vue({ el:"#app", data:{ name:"dk" }, store:myStore, mounted:function(){ console.log(this) } }) </script></html>
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title></head><script src="./js/vuex.js"></script><script src="./js/vue2.0.js"></script><body> <div id="app"> <hello></hello> </div></body><script> Vue.use(Vuex); var myStore = new Vuex.Store({ state:{ //存放组件之间共享的数据 name:"jjk", age:18, num:1 }, mutations:{ //显式的更改state里的数据 change:function(state,a){ // state.num++; console.log(state.num += a); } }, getters:{ getAge:function(state){ return state.age; } }, actions:{ // } }); Vue.component('hello',{ template:"<p @click='changeNum'>姓名:{{name}} 年龄:{{age}} 次数:{{num}}</p>", computed: { name:function(){ return this.$store.state.name }, age:function(){ return this.$store.getters.getAge }, num:function(){ return this.$store.state.num } }, mounted:function(){ console.log(this) }, methods: { changeNum: function(){ //在组件里提交 // this.num++; this.$store.commit('change',10) } }, data:function(){ return { // num:5 } } }) new Vue({ el:"#app", data:{ name:"dk" }, store:myStore, mounted:function(){ console.log(this) } }) </script></html>
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title></head><script src="./js/vuex.js"></script><script src="./js/vue2.0.js"></script><body> <div id="app"> <hello></hello> </div></body><script> Vue.use(Vuex); var myStore = new Vuex.Store({ state:{ //存放组件之间共享的数据 name:"jjk", age:18, num:1 }, mutations:{ //显式的更改state里的数据 change:function(state,a){ // state.num++; console.log(state.num += a); }, changeAsync:function(state,a){ console.log(state.num +=a); } }, getters:{ getAge:function(state){ return state.age; } }, actions:{ //设置延时 add:function(context,value){ setTimeout(function(){ //提交事件 context.commit('changeAsync',value); },1000) } } }); Vue.component('hello',{ template:` <div> <p @click='changeNum'>姓名:{{name}} 年龄:{{age}} 次数:{{num}}</p> <button @click='changeNumAnsyc'>change</button> </div>`, computed: { name:function(){ return this.$store.state.name }, age:function(){ return this.$store.getters.getAge }, num:function(){ return this.$store.state.num } }, mounted:function(){ console.log(this) }, methods: { changeNum: function(){ //在组件里提交 // this.num++; this.$store.commit('change',10) }, //在组件里派发事件 当点击按钮时,changeNumAnsyc触发-->actions里的add函数被触发-->mutations里的changeAsync函数触发 changeNumAnsyc:function(){ this.$store.dispatch('add', 5); } }, data:function(){ return { // num:5 } } }) new Vue({ el:"#app", data:{ name:"dk" }, store:myStore, mounted:function(){ console.log(this) } }) </script></html>
actions:{ add:function(context,value){ setTimeout(function(){ context.commit('changeAsync',value); },1000) } }