vuex实现不同组件的数据共享 数据持久化
当我们清除congsole和network后,刷新一次,算是第一次请求,然后切换选项卡,当再回到用户后,没有再请求接口数据,只是加载vuex里的数据,即缓存到list里的数据.
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
// 在vuex中用于存储数据
var state = {
count: 1,
list: []
}
// mutations里是放方法,主要改变state里的数
var mutations = {
incCount () {
state.count++
},
// 要传值,必须写state
addList (state, data) {
state.list = data
}
}
// getter 有点类似计算属性 改变state里面的count数据的时候会触发 getters里面的方法 获取新的值 (基本用不到)
var getters = {
computedCount (state) {
return state.count * 2
}
}
/*
4、 基本没有用
Action 类似于 mutation,不同在于:
Action 提交的是 mutation,而不是直接变更状态。
Action 可以包含任意异步操作。
*/
var actions = {
// 因此你可以调用 context.commit 提交一个 mutation
incMutationsCount (context) {
// 执行 mutations 里面的incCount方法 改变state里面的数据
context.commit('incCount')
}
}
// 实例化vuex的store
const store = new Vuex.Store({
state,
mutations,
getters,
actions
})
export default store
<template>
<!-- 所有的内容要被根节点包含起来 -->
<div id="home">
我是首页组件
<button @click="goNews()">通过js跳转到新闻页面</button>
<mt-button type="default">default</mt-button>
<br>
<mt-button type="primary">primary</mt-button>
<mt-index-list>
<mt-index-section index="A">
<mt-cell title="Aaron"></mt-cell>
<mt-cell title="Alden"></mt-cell>
<mt-cell title="Austin"></mt-cell>
</mt-index-section>
<mt-index-section index="B">
<mt-cell title="Baldwin"></mt-cell>
<mt-cell title="Braden"></mt-cell>
</mt-index-section>
<mt-index-section index="C">
<mt-cell title="ccc"></mt-cell>
<mt-cell title="ccccc"></mt-cell>
</mt-index-section>
<mt-index-section index="D">
<mt-cell title="DDD"></mt-cell>
<mt-cell title="DDDD"></mt-cell>
</mt-index-section>
<mt-index-section index="Z">
<mt-cell title="Zack"></mt-cell>
<mt-cell title="Zane"></mt-cell>
</mt-index-section>
</mt-index-list>
<hr>
<h2>调用store里的数据{{this.$store.state.count}}----{{this.$store.getters.computedCount}}</h2>
<button @click="changeCount()">改变store里的值</button>
</div>
</template>
<script>
// 引入store
import store from '../Vuex/store'
export default {
data () {
return {
msg: '我是一个home组件'
}
},
// 注册store
store,
methods: {
goNews () {
this.$router.push({ name: 'news' })
},
changeCount () {
// 改变vuex store里的值
// 触发vuex store 里的mutations里的incCount方法
this.$store.commit('incCount')
}
}
}
</script>
<style lang="scss" scoped>
</style>
<template>
<div id="user">
我是用户组件
<br>
<v-actionsheet></v-actionsheet>
<br>
<mt-button @click.native="flag = true" size="large">选择用户头像</mt-button>
<mt-actionsheet :actions="actions" v-model="flag"></mt-actionsheet>
<hr>
<h2>调用store里的数据{{this.$store.state.count}}</h2>
<button @click="changeCount()">改变store里的值</button>
<hr>
<ul>
<li v-for="(item,index) in list" :key=index>
{{item.title}}
</li>
</ul>
</div>
</template>
<script>
import store from '../Vuex/store'
import ActionSheet from './ActionSheet.vue'
export default {
data () {
return {
msg: '我是一个用户组件',
list: [],
flag: false,
actions: []
}
},
// 注册store
store,
components: {
'v-actionsheet': ActionSheet
},
methods: {
takePhoto () {
alert('执行拍照')
},
openAlbum () {
alert('打开相册')
},
changeCount () {
// 改变vuex store里的值
// 触发vuex store 里的mutations里的incCount方法
this.$store.commit('incCount')
},
requestData () {
// 请求数据
var api = 'http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1'
this.$http.get(api).then((response) => {
console.log(response)
// 注意this指向
this.list = response.body.result
// 数据放在store里面
this.$store.commit('addList', response.body.result)
}, function (err) {
console.log(err)
})
}
},
mounted () {
// 判断 store里面有没有数据
var listData = this.$store.state.list
console.log(listData.length)
if (listData.length > 0) {
this.list = listData
} else {
this.requestData()
}
this.actions = [{
name: '拍照',
method: this.takePhoto
}, {
name: '从相册中选择',
method: this.openAlbum
}]
}
}
</script>
<style lang="scss" scoped>
</style>
<template>
<div id="app">
<router-link to="/home">首页</router-link>
<router-link to="/news">新闻</router-link>
<router-link to="/user">用户</router-link>
<router-view />
</div>
</template>
<script>
export default {
name: 'App'
}
</script>
<style lang='scss' scoped>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
</style>