antdesign——切换主题drawer

17 篇文章 1 订阅

在这里插入图片描述

使用组件drawer的一个属性handle可以直接生成这个设置关闭按钮(slot的方式)

在这里插入图片描述

 	<a-drawer
      title="设置"
      placement="right"
      :closable="false"
      :visible="visible"
    >
      <!-- 1、handle的用法 -->
      <template v-slot:handle>
        <a-icon
          class="set"
          :type="visible ? 'close' : 'setting'"
          @click="visible = !visible"
        />
      </template>
      <!-- 设置主题 -->
      <div>
        <h3>设置主题</h3>
        <a-radio-group :value="theme" @change="onChange($event, 'theme')">
          <a-radio value="dark"> 黑色</a-radio>
          <a-radio value="light"> 白色 </a-radio>
        </a-radio-group>
      </div>
    </a-drawer>

切换主题色之后,将主题色存在path的query参数里,为了分享地址的时候,对方可以看到和我们一样的设置

	onChange(e, type) {
      // 将设置的主题的位置设置在路由的query参数,目的是为了将路径分享出去后可以保持跟当前设置一样的效果
      this.$router.push({
        query: { ...this.$route.query, [type]: e.target.value },
      });
    },

其他样式根据主题色修改样式,定义动态类名

<a-layout-sider :class="`theme-${theme}`" width="250" v-model="collapsed">
   <SiderMenu />
</a-layout-sider>
      
.theme-dark {
  background-color: rgb(10, 10, 10);
}
.theme-light {
  background-color: #fff;
}

设置主题源码

设置组件

<template>
  <div>
    <a-drawer
      title="设置"
      placement="right"
      :closable="false"
      :visible="visible"
    >
      <!-- 1、handle的用法 -->
      <template v-slot:handle>
        <a-icon
          class="set"
          :type="visible ? 'close' : 'setting'"
          @click="visible = !visible"
        />
      </template>
      <!-- 设置主题 -->
      <div>
        <h3>设置主题</h3>
        <a-radio-group :value="theme" @change="onChange($event, 'theme')">
          <a-radio value="dark"> 黑色</a-radio>
          <a-radio value="light"> 白色 </a-radio>
        </a-radio-group>
      </div>
    </a-drawer>
  </div>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
    };
  },
  computed: {
    theme() {
      return this.$route.query.theme || "dark";
    },
  },
  mounted() {},
  methods: {
    onChange(e, type) {
      // 将设置的主题的位置设置在路由的query参数,目的是为了将路径分享出去后可以保持跟当前设置一样的效果
      this.$router.push({
        query: { ...this.$route.query, [type]: e.target.value },
      });
    },
  },
};
</script>
<style lang="less" scoped>
.set {
  position: absolute;
  top: 50%;
  right: 256px;
  font-size: 22px;
  color: #fff;
  width: 50px;
  height: 50px;
  background: #136bef;
  line-height: 50px;
  text-align: center;
  border-radius: 4px;
  margin-top: -25px;
}
</style>

父组件

<template>
  <div>
    <a-layout>
      <a-layout-sider :class="`theme-${theme}`" width="250" v-model="collapsed">
        <SiderMenu />
      </a-layout-sider>
      <a-layout>
        <a-layout-header><Header /></a-layout-header>
        <a-layout-content><router-view></router-view></a-layout-content>
        <a-layout-footer><Footer /></a-layout-footer>
      </a-layout>
    </a-layout>
    <!-- 设置主题 -->
    <SettingDrawer />
  </div>
</template>

<script>
import Header from "./Header";
import Footer from "./Footer";
import SiderMenu from "./SiderMenu";
import SettingDrawer from "@/components/SettingDrawer";
import { mapState } from "vuex";
export default {
  data() {
    return {};
  },
  computed: {
    ...mapState(["collapsed"]),
    theme() {
      return this.$route.query.theme || "dark";
    },
  },
  components: {
    Header,
    Footer,
    SiderMenu,
    SettingDrawer,
  },
  mounted() {},
  methods: {},
};
</script>
<style lang="less" scoped>
/deep/.ant-layout {
  height: 100vh;
}
/deep/.ant-layout-content {
  min-height: auto !important;
}
.theme-dark {
  background-color: rgb(10, 10, 10);
}
.theme-light {
  background-color: #fff;
}
</style>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小曲曲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值