Vue vuex的安装和使用

1.控制台输入:npm install vuex

在这里插入图片描述

2.新建store.js文件用于配置vuex

在这里插入图片描述

import vuex from 'vuex'
import  Vue from 'vue'

//使用vuex插件
Vue.use(vuex)

//创建对象
const store = new vuex.Store({
  state:{
    count:1000
  },
  mutations:{

  },
  actions:{
    //异步操作就在这里发起请求
  },
  getters:{

  },
  modules:{

  }
})

//导出对象
export default store

3.vuex的使用

hello.vue中使用vuex定义的全局状态属性

<template>
  <div>
    <h2>{{$store.state.count}}</h2>
  </div>
</template>

<script>
export default {
  name: "Hello",
  props:['count']
}
</script>

<style scoped>

</style>

app.vue中使用vuex定义的全局状态属性
<template>
  <div id="app">
    <h2>{{message}}</h2>
    <h2>{{$store.state.count}}</h2>
<!-- 最好不要这样改状态 -->
    <button @click="$store.state.count++">+</button>
    <button @click="$store.state.count--">-</button>

    <h2>------------ hello ---------------</h2>
    <Hello :count="count"></Hello>
  </div>
</template>

<script>
import Hello from "@/components/Hello";
export default {
  name: 'App',
  components:{
    Hello
  },
  data(){
    return{
      message:'app组件',
      count:0
    }
  }
}
</script>

<style>

</style>

注意:不要在组件中修改store.js中定义的全局状态属性

4.浏览器安装 Vue.js devtools 插件

在这里插入图片描述

F12查看有没有vue选项,有的话就安装成功了

在这里插入图片描述
这里查看vuex状态
在这里插入图片描述

附上使用教程 Vuex 使用总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值