Vue学习之旅----vuex不同组件间数据共享-状态一致

<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>
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
// 在vuex中用于存储数据
var state = {
  count: 1
}
// mutations里是放方法,主要改变state里的数
var mutations = {
  incCount () {
    state.count++
  }
}
// 实例化vuex的store
const store = new Vuex.Store({
  state,
  mutations
})
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}}</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="news">
    我是用户组件
    <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>
  </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')
    }
  },
  mounted () {
    this.actions = [{
      name: '拍照',
      method: this.takePhoto
    }, {
      name: '从相册中选择',
      method: this.openAlbum
    }]
  }
}
</script>
<style lang="scss" scoped>
</style>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑞朋哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值