码迷,mamicode.com
首页 > 其他好文 > 详细

vuex初识

时间:2019-10-14 17:31:00      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:ons   smooth   world   use   tin   class   config   ali   top   

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初识

标签:ons   smooth   world   use   tin   class   config   ali   top   

原文地址:https://www.cnblogs.com/Harold-Hua/p/11672585.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!