<!-- vuex 配置js store.js -->
1.引入vue和vuex

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);
const store= new Vuex.Store({
<!-- 用来放数据 -->
  state:{
    user:{
      name:'768028030@qq.com',
      age:'24',
      phone:'13370123965'
    },
    type:{
      status:true,
      swichText:"隐藏"
    },

    text:"这个样的数据"
  },

  //在组件中获取state数据的最简单方法是通过 计算属性直接返回 computed: { count () { return this.$store.state.text} }
  //一个组件中用到多个state数据是 可以使用 mapState辅助函数 (记得先import哦!  酱紫:import { mapState } from 'vuex')
<!-- -->
  getters: {
    name: ({user}) => user.name,
    age: ({user}) => user.age,
    phone:({user})=>user.phone,
    type:({type})=>type,
    mytext:(state)=>state.text,

  },
<!-- 类似事件 用来更改state的数据 重点是需要用actions提交哦 -->
  mutations:{
    increment ({user}) {
      // 变更状态
      user.age++
    },
    setType ({type}){
      if(type.status==true){
        type.status=false;
        type.swichText="显示"
      }else{
        type.status=true;
        type.swichText="隐藏"
      }
    }
  },
<!-- 用来提交mutations -->
  actions:{
    increment ({ commit }) {
<!-- 类似事件注册 -->
      commit('increment')
    },
    setType ({ commit }) {
      commit('setType')
    }
  }
})

export default store;

<!--使用数据 hello.vue-->

<template>
  <div class="hello">
    <img src="../assets/why.jpg"/>
    <p>{{$store.getters.name}}</p>
    <p>{{$store.getters.age}}</p>
    <p @click="increment">点击加1</p>
    <p>{{$store.getters.type.status}}</p>
    <p @click="setType">{{$store.getters.type.swichText}}</p>
  </div>
</template>

<script>
  import {mapActions} from 'vuex'
  export default {
    name: 'hello',
    data () {
      return {
        msg: 'Welcome to Your Vue.js App'
      }
    },
    created (){
      this.openNew();
    },
    methods: {
      openNew(){
        alert(1)
      },
      ...mapActions([
        'increment',
        'setType'
      ])
    }
  }
</script>

  

最新文章

  1. zmap在阿里云主机上的编译
  2. 库函数API和C语言汇编语言混合式编程
  3. 2017-1-2 nfs服务器配置
  4. css 去除点击之后的虚线
  5. CentOS6.5安装Tab增强版:bash-completion
  6. 【leetcode❤python】 400. Nth Digit
  7. MotionEvent
  8. java如何判断字符串是否为空的方法
  9. (转) Crittercism: 在MongoDB上实现每天数十亿次请求
  10. label的for属性与inputde的id元素绑定
  11. xml中,button改变背景颜色方法
  12. DBCC用法汇总
  13. ps -ef |grep 输出的具体含义
  14. strtok函数 分类: c++ 2014-11-02 15:24 214人阅读 评论(0) 收藏
  15. [HNOI2009]最小圈(分数规划+SPFA判负环)
  16. vue-cli 报Module build failed: Error: No parser and no file path given, couldn&#39;t infer a parser.错的解决方法
  17. 30.QT-渐变之QLinearGradient、 QConicalGradient、QRadialGradient
  18. svn 修改文件的可执行权限
  19. vlanif和vlan路由
  20. log4配置

热门文章

  1. maven 中配置多个mirror的问题
  2. SQL触发器中的inserted表和deleted表
  3. 任务调度Quartz.Net之Windows Service
  4. JVM(二) 栈内存结构
  5. python glob删除目录下的文件
  6. xorm表结构操作实例
  7. CH08 QSPI启动并从EMMC运行APP
  8. PB数据窗口分页
  9. java.lang.RuntimeException: Unable to instantiate activity ComponentInfo异常解决
  10. Integer源码解析