一个vuex的完整使用

1、图中的vuex区域步骤每一步都不可少

  • 安装vuex npm install vuex --save
  • 引入vue vuex 并进行挂载
  • import Vue from ‘vue’
    import Vuex from ‘vuex’
    Vue.use(Vuex)

2、

在这里插入图片描述
3、

<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{this.$store.state.city}}</div>
          </div>

        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper" v-for="item of hot" :key="item.id" @click="handleCityClick(item.name)">
            <div class="button">{{item.name}}</div>
          </div>

        </div>
      </div>
      <div class="area" v-for="(item , key) of cities" :key="key" :ref="key">
        <div class="title  border-topbottom">{{key}}</div>
        <div class="item-liast">
          <div class="item border-bottom" v-for="innerItem of item" :key="innerItem.id">{{innerItem.name}}</div>

        </div>

      </div>

    </div>
  </div>
</template>
<script>
import Bscroll from "better-scroll";
export default {
  name: "List",
  props: {
    hot: Array,
    cities: Object,
    letter: String,
  },
  methods: {
    handleCityClick(city) {
      // console.log(city);
      this.$store.dispatch("changeCity", city);
    },
  },

  mounted() {
    // console.log(this.scroll);
    this.scroll = new Bscroll(this.$refs.wrapper);
  },
  watch: {
    letter(e) {
      // console.log(e);
      const element = this.$refs[this.letter][0];
      this.scroll.scrollToElement(element);
      // console.log(element);
    },
  },
};
</script>
<style lang="stylus" scoped>
@import '~style/varibles.styl';

.border-topbottom {
  &:before {
    border-color: #777;
  }

  &:after {
    border-color: #777;
  }
}

.border-bottom {
  &:before {
    border-color: #777;
  }
}

.list {
  overflow: hidden;
  position: absolute;
  left: 0;
  top: 1.58rem;
  right: 0;
  bottom: 0;
}

.button-list {
  overflow: hidden;
  padding: 0.1rem 0.6rem 0.1rem 0.1rem;
}

.button-wrapper {
  float: left;
  width: 33.33%;
}

.button {
  text-align: center;
  margin: 0.1rem;
  border: 0.02rem solid #ccc;
  padding: 0.1rem 0;
  border-radius: 0.06rem;
}

.title {
  line-height: 0.44rem;
  background-color: #eee;
  padding-left: 0.2rem;
  color: #666;
  font-size: 0.26rem;
}

.item-liast .item {
  line-height: 0.76rem;
  padding-left: 0.2rem;
}
</style>

js文件

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    city: '北京'
  },
  actions: {
    changeCity(ctx, city) {
      // console.log(ctx);
      // console.log(city);
      ctx.commit('changeCitys', city)

    }
  },
  mutations: {
    changeCitys(state, city) {
      state.city = city
    }
  }
})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
完整Vuex Store 流程如下: 1. 安装 Vuex使用 npm 或 yarn 安装 Vuex 到你的项目中。 2. 创建 Store 实例:在应用程序的入口文件中引入 Vuex,并创建一个新的 Vuex Store 实例。在实例中定义 state、mutations、actions 和 getters。 3. 注入 Store:将创建的 Store 实例注入到根 Vue 实例中,以便在整个应用程序中访问和使用该 store。 4. 在组件中使用 Store:在组件中使用 `this.$store` 访问 Vuex Store 的各种方法和属性。 - 使用 `this.$store.state` 来获取状态数据。 - 使用 `this.$store.commit('mutationName', payload)` 来触发 mutation。 - 使用 `this.$store.dispatch('actionName', payload)` 来触发 action。 - 使用 `this.$store.getters.getterName` 来获取 getter 的值。 5. 在 mutations 中同步更改状态:在 mutations 中定义同步的方法,用于更改 state 中的数据。通过提交(commit)一个 mutation 来触发这些方法。 6. 在 actions 中处理异步操作:在 actions 中定义异步的方法,用于处理异步操作,并通过调用 mutations 中的方法来更改 state。 7. 使用 getters 派生状态:在 getters 中定义派生出一些状态的方法,通过调用这些方法来获取派生状态的值。 以上就是使用 Vuex Store 的完整流程。通过 Vuex,你可以更好地组织和管理应用程序的状态,并实现数据的共享和响应式更新。希望对你有所帮助!如果你还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值