Copy < template >
< div >
{{ result }}
</ div >
</ template >
< script >
export default {
name : 'AppCount' ,
computed : {
result (){
return this . $store . state .result;
}
}
}
</ script >
Copy < template >
< div >
< button @click = "increment" >Increment</ button >
< button @click = "decrement" >Decrement</ button >
</ div >
</ template >
< script >
export default {
name : 'AppCount' ,
methods : {
increment () {
this . $store . state .result ++
} ,
decrement () {
this . $store . state .result --
}
}
}
</ script >
Copy < template >
< div >
<!-- <p>{{ result }}</p> -->
<!-- <p>{{ nameResult }}</p> -->
< p style = "color: red;" >{{ testResult }}</ p >
< p style = "color: green;" >{{ nameResult }}</ p >
</ div >
</ template >
< script >
import {mapGetters} from 'vuex' ;
export default {
name : 'AppCount' ,
// computed: {
// result() {
// return this.$store.getters.testResult
// },
// nameResult() {
// return this.$store.getters.nameResult
// }
// },
// Use short
computed : {
... mapGetters ([ 'testResult' , 'nameResult' ])
}
}
</ script >
Copy import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export const store = new Vuex.Store({
state: {
result: 0
},
getters: {
testResult: state => {
return state.result * 10;
},
nameResult: state => {
return state.result * 10 + " name products";
}
}
})
Copy import Vue from 'vue'
import App from './App.vue'
import {store} from './store.js'
Vue.config.productionTip = false
new Vue({
store,
render: h => h(App),
}).$mount('#app')
Copy {
"name" : "Aaaaaaaa" ,
"version" : "0.1.0" ,
"private" : true ,
"scripts" : {
"serve" : "vue-cli-service serve" ,
"build" : "vue-cli-service build" ,
"lint" : "vue-cli-service lint"
} ,
"dependencies" : {
"core-js" : "^3.8.3" ,
"vue" : "^2.6.14" ,
"vue-router" : "3.6.5" ,
"vuex" : "2.0.0"
} ,
"devDependencies" : {
"@babel/core" : "^7.12.16" ,
"@vue/cli-plugin-babel" : "~5.0.0" ,
"@vue/cli-service" : "~5.0.0" ,
"vue-template-compiler" : "^2.6.14"
} ,
"eslintConfig" : {
"root" : true ,
"env" : {
"node" : true
} ,
"extends" : [
"plugin:vue/essential" ,
"eslint:recommended"
] ,
"parserOptions" : {
"parser" : "@babel/eslint-parser"
} ,
"rules" : {}
} ,
"browserslist" : [
"> 1%" ,
"last 2 versions" ,
"not dead"
]
}ht