Vuex系列状态管理篇--Vuex(3)之getting

4、Vuex–getters

相当于数据仓库的计算属性

4.1、在根目录下

  1. 定义 在文件 getters.js 中直接定义
const getters = {
  appWithVersion: (state) => {
    return `${state.appName}v2.0`
  }
}

export default getters;
  1. 使用
this.$store.getters.appWithVersion

使用 vuex 的 mapGetters

import { mapState, mapGetters } from "vuex";

...mapGetters([
     'appWithVersion'
 ])

4.2、在模块下

1、定义

const state = {
  userName: 'pig'
}
const getters = {
  firstLetter: (state) => {
    return state.userName.substr(0, 1)
  }
}

2、使用

使用 vuex 的mapGetters方法

import { mapState, mapGetters } from "vuex";

...mapGetters('user', [  // 要添加模块名
      'firstLetter'
])

也可以使用命名空间的方法

import { createNamespacedHelpers } from 'vuex'
const { mapGetters } = createNamespacedHelpers('user')

 ...mapGetters([
     'firstLetter'
 ])
使用 UniApp(一款支持多平台快速开发的应用框架)结合 Vue3 进行深色主题的代码编写,你可以按照以下步骤操作: 首先,在项目中安装 UniApp 的 Vue3 版本,可以参考其官方文档:https://uniapp.dcloud.net.cn/docs/v3/getting-started/installation 1. 设置 Vue3 配置文件:在 `uni` 目录下的 `project.config.js` 文件中,配置默认的主题,例如设置为深色模式: ```javascript export default { //... framework: { config: { //... theme: &#39;dark&#39;, // 设置为 dark 表示深色模式 }, }, }; ``` 2. 定义全局样式:在 `src/styles/global.scss` 或 `global.less` 中,添加基础深色主题样式,如颜色变量、字体等: ```scss // global.scss (假设使用 SCSS) $primary-color: #37474F; $secondary-color: #6E8590; // 在这里添加更多的颜色和布局样式... body { background-color: $primary-color; color: white; } /* 省略其他元素的样式 */ ``` 3. 主题切换功能(可选):如果你希望用户能手动切换主题,可以在页面上添加一个开关按钮,并监听状态变化: ```html <template> <view class="theme-switch"> <button @click="toggleTheme">切换主题</button> </view> </template> <script> export default { methods: { toggleTheme() { this.$store.commit(&#39;toggleTheme&#39;); // 使用 Vuex 存储主题状态并更新 } }, computed: { isDarkTheme() { return this.$store.state.theme === &#39;dark&#39;; } }, }; </script> ``` 配合 Vue Router,确保在路由守卫中更新主题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值