vuex中的state

简介: vuex中的state

vuex中的state

简介

本文讲解vuex中的state使用方法。

入门讲解

首先第一步肯定是创建vue项目,具体操作看这篇文章:用命令窗口的方式创建Vue项目

首先在store文件夹下面,创建一个state.js文件,存放公共的数据

在store文件夹下面的index.js文件中进行如下配置。

然后前端可以采取这两种写法,就可以访问到存储的数据了。所以我们可以知道的是这个state.js就是用来存放数据的。

mapState 辅助函数

具体代码如下:

<template>
  <div class="about">
    <h1>This is an about page</h1>
    {{ $store.state.str }}
    <hr>
    {{ str }}
    <hr>
    {{ a }}
    <hr>
    {{ b }}
  </div>
</template>
<script>
import { mapState } from 'vuex'
export default {
  computed: {
    ...mapState(['str', 'a', 'b'])
  }
}
</script>

运行结果:

案例

好的,在不增加难度的情况下,我来给您修改一下之前的案例。

案例 1:在线状态

思路
const store = new Vuex.Store({
  state: {
    onlineStatus: false
  }
})

这里我们定义了一个名为 onlineStatus 的属性,并初始化为 false。

当用户上线时,可以更新 onlineStatus 属性:

store.state.onlineStatus = true

这将直接更改 onlineStatus 属性中的值。

然后,你可以通过其他组件调用此值:

computed: {
  onlineStatus() {
    return this.$store.state.onlineStatus
  }
}
完整代码

项目结构

  • state.js
export default {
  onlineStatus: false
}
  • index.js
import { createStore } from 'vuex'
import state from './state'
export default createStore({
  state,
  mutations: {
    SET_ONLINE_STATUS (state, status) {
      state.onlineStatus = status
    }
  }
})
  • TestView.vue
<template>
    <div>
      <p>Your online status is {{ onlineStatusText }}</p>
    </div>
  </template>
<script>
export default {
  computed: {
    onlineStatusText () {
      return this.$store.state.onlineStatus ? 'Online' : 'Offline'
    }
  }
}
</script>
  <style>
  /* 样式可以选择添加 */
  </style>

案例 2:主题样式

思路
const store = new Vuex.Store({
  state: {
    themeColor: 'blue'
  }
})

我们定义了一个名为 themeColor 的属性,并用 "blue" 初始化它。为了更改主题颜色,可以直接设置 themeColor 的值:

store.state.themeColor = 'red'

这将直接更改我们的主题颜色值。

然后,你可以通过其他组件调用此值:

computed: {
  themeColor() {
    return this.$store.state.themeColor
  }
}
完整代码
  • state.js
export default {
  onlineStatus: false,
  themeColor: 'blue'
}
  • index.js
import { createStore } from 'vuex'
import state from './state'
export default createStore({
  state,
  mutations: {
    SET_ONLINE_STATUS (state, status) {
      state.onlineStatus = status
    },
    SET_THEME_COLOR (state, color) {
      state.themeColor = color
    }
  }
})
  • TestView.vue
<template>
    <div :style="{ background: themeColor }">
      <p>Your theme color is {{ themeColor }}</p>
      <button @click="changeThemeColor">Change Theme Color</button>
    </div>
  </template>
<script>
export default {
  computed: {
    themeColor () {
      return this.$store.state.themeColor
    }
  },
  methods: {
    changeThemeColor () {
      this.$store.state.themeColor = 'red'
    }
  }
}
</script>
  <style>
  /* 样式可以自定义 */
  </style>
  • 运行结果

相关文章
|
4月前
|
JavaScript
v-model绑定vuex的state
v-model绑定vuex的state
|
10月前
|
前端开发 JavaScript BI
轻松搞定vue3+Pinia-2-修改state-patch-actions
轻松搞定vue3+Pinia-2-修改state-patch-actions
219 0
|
4月前
|
前端开发 数据处理 开发者
vuex中mutations详解,与actions的区别
Vuex 的 Mutations 是用于改变 Vuex Store 中状态的一种方式。它是一个同步的操作,用于直接修改 Store 中的状态。
|
1月前
|
JavaScript
成功解决:[vuex] must call Vue.use(Vuex) before creating a store instance.
这篇文章介绍了在使用Vue和Vuex时遇到的一个常见错误:"[vuex] must call Vue.use(Vuex) before creating a store instance." 错误的原因是在使用顺序上出现了问题,即在创建store之前没有正确地声明使用vuex。文章提供了详细的解决方法,即将`Vue.use(Vuex)`直接放在`store/index.js`文件中,以确保在创建store实例之前Vuex已经被Vue使用。通过这种方式,问题得到了成功解决。
成功解决:[vuex] must call Vue.use(Vuex) before creating a store instance.
|
2月前
|
存储 JavaScript
VUEX 的使用学习二: state
VUEX 的使用学习二: state
27 0
|
4月前
|
JavaScript
如何在Vuex中定义和使用getters和actions
在Vuex中,`getters`用于派生状态值,类似Vue的计算属性,例如从`todos`状态中过滤已完成/未完成任务。`actions`则处理异步操作,如模拟加载数据,通过`commit`改变状态。
|
10月前
vue3中Pinia的使用之state
vue3中Pinia的使用之state
|
4月前
|
JavaScript 前端开发 测试技术
vuex中Actions详解
- Actions 是 Vuex 中的一个重要概念,用于处理异步操作和触发mutations。 - Actions 可以包含任意的 JavaScript 逻辑,包括异步操作(如发送 AJAX 请求)。 - Actions 通过调用 store.dispatch 方法来触发mutations。
|
4月前
|
前端开发
React State(状态)
React State(状态)
|
存储 前端开发
React中的props和state
React中的props和state
80 0