!+Cookies.get(‘sidebarStatus’) 详解

项目部分代码(app.js)

import Cookies from 'js-cookie'

const app = {
  state: {
    sidebar: {
      opened: !+Cookies.get('sidebarStatus'),
      withoutAnimation: false
    },
    themeStatus: Cookies.get('themeStatus') === undefined ? 1 : Cookies.get('themeStatus')
  },
  mutations: {
    TOGGLE_SIDEBAR: state => {
      if (state.sidebar.opened) {
        Cookies.set('sidebarStatus', 1)
      } else {
        Cookies.set('sidebarStatus', 0)
      }
      state.sidebar.opened = !state.sidebar.opened
      state.sidebar.withoutAnimation = false
    },
    CLOSE_SIDEBAR: (state, withoutAnimation) => {
      Cookies.set('sidebarStatus', 1)
      state.sidebar.opened = false
      state.sidebar.withoutAnimation = withoutAnimation
    },
  },
  actions: {
    ToggleSideBar: ({ commit }) => {
      commit('TOGGLE_SIDEBAR')
    },
    CloseSideBar ({ commit }, { withoutAnimation }) {
      commit('CLOSE_SIDEBAR', withoutAnimation)
    },
  },
  getters: {
    themeStatus: state => state.themeStatus
  }
}

export default app

为什么要这样写?
因为cookie取出的值都是string类型, 但是代码中需要用的是布尔类型
在这里插入图片描述

Cookies.get(‘sidebarStatus’))的意思是取出来的是"1"或 “0”
+Cookies.get(‘sidebarStatus’)的意思是+"1"或 +“0” , 触发隐式类型转换, 会得到1或者 0
!+Cookies.get(‘sidebarStatus’))的意思是!1或 !0 , 对数值类型取反会返回布尔类型, 会得到 false或true
!!+Cookies.get(‘sidebarStatus’)的意思是!false或 !true , 对布尔值进行取反, 会得到对应的布尔值

扩展知识
六大假值:0,undefined,NaN,null,""(空字符串),flase
字符串类型转换为数字类型:parseFloat(),parseInt(),Number(),+等等

在这里插入图片描述

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值