counter.vue
<template>
<div>
{{ number }}
<button @click="handleAddRandom">随机增加</button>
</div>
</template>
<script>
export default {
props: {
number: {
type: Number
}
},
methods: {
handleAddRandom () {
const num = Math.floor(Math.random () * 100 + 1);
this.$bus.emit('add', num);
}
}
};
</script>
app.vue
<template>
<div>
<h1>首页</h1>
<p>{{ count }}</p>
<button @click="handleIncrement">+1</button>
<button @click="handleDecrease">-1</button>
<button @click="handleIncrementMore">+5</button>
<button @click="handleActionIncrement">action +1</button>
<button @click="handleAsyncIncrement">async +1</button>
<p> 随机增加:<Counter :number="number"></Counter><p>
<div>{{ list }}</div>
<div>{{ listCount }}</div>
</div>
</template>
<script>
import Counter from './counter.vue';
export default {
components: {
Counter
},
computed: {
count () {
return this.$store.state.count;
},
list () {
return this.$store.getters.filteredList;
},
listCount () {
return this.$store.getters.listCount;
}
},
methods: {
handleIncrement () {
this.$store.commit('increment');
},
handleDecrease () {
this.$store.commit('decrease');
},
handleIncrementMore () {
this.$store.commit('increment', 5);
},
handleActionIncrement () {
this.$store.dispatch('increment')
},
handleAsyncIncrement () {
this.$store.dispatch('asyncIncrement').then(() => {
console.log(this.$store.state.count);
});
},
handleAddRandom (num) {
this.number += num;
}
},
data () {
return {
number: 0
}
},
created () {
this.$bus.on('add', this.handleAddRandom);
},
beforeDestroy () {
this.$bus.off('add', this.handleAddRandom);
}
}
</script>
main.js
import Vue from 'vue';
import VueRouter from 'vue-router';
import Vuex from 'vuex';
import VueBus from './vue-bus';
import App from './app.vue';
Vue.use(VueRouter);
Vue.use(Vuex);
Vue.use(VueBus);
const Routers = [
{
path: '/index',
meta: {
title: '首页'
},
component: (resolve) => require(['./views/index.vue'], resolve)
},
{
path: '/about',
meta: {
title: '关于'
},
component: (resolve) => require(['./views/about.vue'], resolve)
},
{
path: '/user/:id',
meta: {
title: '个人主页'
},
component: (resolve) => require(['./views/user.vue'], resolve)
},
{
path: '*',
redirect: '/index'
}
];
const RouterConfig = {
mode: 'history',
routes: Routers
};
const router = new VueRouter(RouterConfig);
router.beforeEach((to, from, next) => {
window.document.title = to.meta.title;
next();
});
router.afterEach((to, from, next) => {
window.scrollTo(0, 0);
});
const store = new Vuex.Store({
state: {
count: 0,
list: [1, 5, 8, 10, 30, 50]
},
getters: {
filteredList: state => {
return state.list.filter(item => item < 20);
},
listCount: (state, getters) => {
return getters.filteredList.length;
}
},
mutations: {
increment (state, n = 1) {
state.count += n;
},
decrease (state) {
state.count --;
}
},
actions: {
increment (context) {
context.commit('increment');
},
asyncIncrement (context) {
return new Promise(resolve => {
setTimeout(() => {
context.commit('increment');
resolve();
}, 1000)
});
}
}
});
new Vue({
el: '#app',
router: router,
store: store,
render: h => {
return h(App)
}
});
|