vuex初识

Posted harold-hua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vuex初识相关的知识,希望对你有一定的参考价值。

main.js

import Vue from ‘vue‘
import App from ‘./App.vue‘
import Vuex from ‘vuex‘

Vue.config.productionTip = false

Vue.use(Vuex)
const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    countIncrease1(state) {
      state.count++
    },
    countIncrease2(state, v) {
      state.count = v;
    }
  }
})

new Vue({
  store,
  render: h => h(App),
}).$mount(‘#app‘)

 

App.vue

<template>
  <div id="app">
    <!-- <img alt="Vue logo" src="./assets/logo.png">
    <HelloWorld msg="Welcome to Your Vue.js App"/>-->
    <h1>count:{{this.$store.state.count}}</h1>
    <h2>count:{{count}}</h2>
    <button @click="countIncrease1">点我自增</button>
    <button @click="countIncrease2">点我改变</button>
  </div>
</template>

<script>
// import HelloWorld from "./components/HelloWorld.vue";

export default {
  name: "app",
  components: {
    // HelloWorld
  },
  computed: {
    count() {
      return this.$store.state.count;
    }
  },
  methods: {
    countIncrease1() {
      this.$store.commit("countIncrease1");
    },
    countIncrease2() {
      const v = 100;
      this.$store.commit("countIncrease2", v);
    }
  }
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

 

以上是关于vuex初识的主要内容,如果未能解决你的问题,请参考以下文章

初识vue 2.0:vuex组件通信

初识vue 2.0:vuex进阶

vuex初识

Vuex初识

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段