vuex语法讲解

state的用法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../../node_modules/vuex/dist/vue.js"></script>
    <script src="../../node_modules/vuex/dist/vuex.min.js"></script>
</head>
<body>
<div id="app">
  <h2>{{msg}}</h2>
  <counter v-bind:count="count"></counter>
</div>
<script>
  const counter = {
      template:`
      <div>{{count}}</div>`,
      computed:{
      count(){
        return this.$store.state.count;

      // props:['count']
  }}}
  const store = new Vuex.Store({
    state:{
      count:10
    }
  });
  new Vue({
  el:"#app",
  store,
  data:{
    msg:"Vuex的使用",
    count:15
  },
  components:{
    counter
  }
  })
</script>
</body>
</html>
<script>
  // import Counter from "../../src/components/Counter";
  // export default {
  //   components: {Counter}
  // }
</script>

 

mutations的使用,点击提交按钮,改变值

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title-mutations</title>
    <script src="../../node_modules/vuex/dist/vue.js"></script>
    <script src="../../node_modules/vuex/dist/vuex.min.js"></script>
</head>
<body>
<div id="app">
  <h2>{{msg}}</h2>
  <a href="javascript:;" @click="add">点击</a>
  <counter></counter>
</div>
<script>
  const counter = {
      template:`<div>
      <div>点击的数量:{{count}}</div>
      <div>用户名:{{name}}</div>
      </div>`,
      computed:{
      count(){
        return this.$store.state.count;
      },
      name(){
          return this.$store.state.name;
        }
  }};
  const store = new Vuex.Store({
    state:{
      count:10,
      name:'java'
    },
    mutations:{
      increment(state,num){
      state.count=num;
      },
      updateName(state,userName){
        state.name=userName;
      }
    }
  });
  new Vue({
  el:"#app",
  store,
  data:{
    msg:"Vuex的使用",

  },
  components:{
    counter
  },
  methods:{
    add(){
    this.$store.commit('increment',100),
    this.$store.commit("updateName",'wang')
    }
  }
  })
</script>
</body>
</html>
<script>
  // import Counter from "../../src/components/Counter";
  // export default {
  //   components: {Counter}
  // }
</script>

actions的使用,action来提交mutations里的数据

,action第一个概念是用来提交mutations,第二个他是异步的,里面可以定义异步,下面讲解怎么去提交mutations

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title-actions</title>
    <script src="../../node_modules/vuex/dist/vue.js"></script>
    <script src="../../node_modules/vuex/dist/vuex.min.js"></script>
</head>
<body>
<div id="app">
  <h2>{{msg}}</h2>
  <a href="javascript:;" @click="add">点击</a>
  <counter></counter>
</div>
<script>
  const counter = {
      template:`
      <div>点击的数量:{{count}}</div>
      `,
      computed:{
      count(){
        return this.$store.state.count;
      },

  }};
  const store = new Vuex.Store({
    state:{
      count:10,

    },
    mutations:{
      increment(state,num){
      state.count=num;
      },

    },
    actions:{
      incrementAction(context,num){
        context.commit("increment",num);
      }
    }
  });
  new Vue({
  el:"#app",
  store,
  data:{
    msg:"Vuex的使用",

  },
  components:{
    counter
  },
  methods:{
    add(){
    this.$store.dispatch("incrementAction",5);
    }
  }
  })
</script>
</body>
</html>
<script>
  // import Counter from "../../src/components/Counter";
  // export default {
  //   components: {Counter}
  // }
</script>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vuex是一个用于管理Vue.js应用程序状态的库。它提供了一种集中式的状态管理方案,可以在应用程序中的所有组件之间共享状态。 mapState是Vuex提供的一个辅助函数,用于将Vuex的state映射到组件的计算属性中。它可以简化在组件中访问和使用state的过程。 使用mapState语法糖可以更方便地将Vuex的state映射到组件。它接受一个数组或对象作为参数,数组中的元素可以是state中的属性名,也可以是一个包含两个属性的对象,分别为state中的属性名和在组件中使用的名称。 下面是使用mapState语法糖的示例: ```javascript import { mapState } from 'vuex' export default { computed: { ...mapState(['count']) // 将state中的count属性映射到组件中的count计算属性 } } ``` 上述示例中,count是state中的属性名,通过在计算属性中使用...mapState(['count']),就可以在组件中直接使用this.count来访问state中的count属性。 除了数组形式,mapState还支持对象形式,可以自定义映射后的名称。例如: ```javascript import { mapState } from 'vuex' export default { computed: { ...mapState({ countValue: 'count' // 将state中的count属性映射到组件中的countValue计算属性 }) } } ``` 上述示例中,count是state中的属性名,countValue是在组件中使用的名称,通过this.countValue来访问state中的count属性。 这就是Vuex语法糖mapState的用法。它可以简化在组件中访问和使用Vuex的state的过程,提高开发效率。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值