Skip to content

Commit

Permalink
增加页面通讯案例,使用 vuex 实现 [issues15](#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
KuangPF committed May 20, 2018
1 parent 2059891 commit 80ddb1c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 4 deletions.
Binary file added docs/_img/vuex01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_img/vuex02.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@

* issues 需求
* [request](request.md)
* [页面通讯](vuex.md)
37 changes: 37 additions & 0 deletions docs/vuex.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# vuex
vuex

## 页面通信
该示例为小程序间的页面通讯,利用 vuex 实现

`mpvue`框架中使用 `vuex` 与在 `vue` 中使用是一样的体验,

1.建立相关的目录结构

![vuex01](_img/vuex01.png)

2.在 src/main.js 中将 store 挂载到 vue 原型下面

``` javascript
Vue.prototype.$store = store;
```

3.在页面中通过 mapGetters 和 mapMutations 使用 state

``` javascript
methods: {
...mapMutations({
setMpvueInfoVuex: 'SET_MPVUEINFO'
})
},

computed: {
...mapGetters([
'mpvueInfo'
])
}
```

**效果**

![vuex01](_img/vuex02.gif)
1 change: 0 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ App.mpType = 'app';
Vue.prototype.$store = store;
import '../static/weui/weui.css'
const app = new Vue({
store,
App
})
app.$mount();
Expand Down
6 changes: 3 additions & 3 deletions src/pages/vuex/index.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<template>
<div class="page">
<div class="page__hd">
<div class="page__title">页面通讯管理</div>
<div class="page__desc">利用 vuex 实现数据的整理</div>
<div class="page__title">页面通讯</div>
<div class="page__desc">利用 vuex 实现数据的管理</div>
</div>
<div class="page__bd page__bd_spacing">
<button class="weui-btn mini-btn" type="primary" size="mini" @click="getMpvueInfo">点击显示 state => mpvueInfo</button>
<button class="weui-btn mini-btn" type="warn" size="mini" @click="commitMpvueInfo">点击修改 state => mpvueInfo</button>
<button class="weui-btn mini-btn" type="warn" size="mini" @click="commitMpvueInfo">点击更新 state => mpvueInfo</button>
<div class="mpvueInfo-desc" v-show="showMpvueInfo">{{mpvueInfo}}</div>
<button type="primary" class="mt-15" @click="turnToVuexPage">点击跳转</button>
</div>
Expand Down

0 comments on commit 80ddb1c

Please sign in to comment.