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>
  • 运行结果

相关文章
|
1天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8048 1
|
10天前
|
域名解析 人工智能 Linux
|
20天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18037 63
|
18天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
4天前
|
人工智能 安全 网络安全
|
2天前
|
人工智能 弹性计算 安全
阿里云 码上2027 AI coding 嘉年华
阿里云“码上2027”AI Coding嘉年华于2026年12月2—3日在上海西岸艺术中心举行,聚焦AI Coding从单点提效迈向企业级研发体系升级。发布Qwen4-max模型、QBot智能协作、AgentCore平台等重磅成果,并推出ECS云服务器特惠(200Mbps流量月付40元起)。
763 0
|
8天前
|
云安全 数据采集 人工智能
|
9天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1453 5