<!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) } }