C:\Users\Administrator\Desktop\imoney\src\components\AppResult.vue
<template>
<div>
{{ result }}
</div>
</template>
<script>
export default {
name: 'AppCount',
computed: {
result(){
return this.$store.state.result;
}
}
}
</script>
C:\Users\Administrator\Desktop\imoney\src\App.vue
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<AppResult />
<br/>
<OtherResult />
<br/>
<app-count />
</div>
</template>
<script>
import AppCount from './components/AppCount.vue'
import AppResult from './components/AppResult.vue'
import OtherResult from './components/OtherResult.vue'
export default {
name: 'App',
components: {
AppResult,
AppCount,
OtherResult
}
}
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
C:\Users\Administrator\Desktop\imoney\src\components\OtherResult.vue
<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>
C:\Users\Administrator\Desktop\imoney\src\components\AppCount.vue
<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++
this.$store.commit('incrementOp',5);
},
decrement() {
this.$store.state.result--
}
}
}
</script>
C:\Users\Administrator\Desktop\imoney\src\store.js
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";
}
},
mutations: {
incrementOp(state,nuber=1) {
state.result += nuber;
}
}
})
C:\Users\Administrator\Desktop\imoney\src\main.js
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')