vue实现多布局模式

news/2024/10/20 20:58:18/

1、目标效果

        源码地址:multipal-layout-demo: vue2实现多布局+暗黑模式

默认布局:头部宽度100%,侧边栏、内容区

顶部布局:头部宽度100%,内容区

侧边栏布局:侧边栏高度100%,头部、内容区

2、原理分析

(1)vuex文件

import Vue from 'vue'
import Vuex from 'vuex'Vue.use(Vuex)export default new Vuex.Store({state: {// 暗黑模式isDark: false,// 布局类型layoutType: 'default'},mutations: {// 修改暗黑模式set_is_dark(state, val) {state.isDark = val},// 修改布局类型set_layout_type(state, val) {state.layoutType = val}},actions: {},modules: {}
})

        

(2)布局缩略图如何实现?用div + css 手动实现布局样式

        父组件传递一个布局类型数组,遍历此组件;用一个变量保存索引值,点击不同的布局类型项时切换索引并在vuex修改当前选中的布局类型

        将缩略图封装成组件:Thumbnail.vue

<template><!-- 缩略图 --><div class="thumbnail"><div class="layout" v-for="(item, index) in layouts" @click="changeCheck(item, index)"><template v-if="item.type == 'default'"><div class="top" :style="{ background: isDark ? 'black' : '#fff' }"></div><div class="left" :style="{ background: isDark ? 'black' : '#fff' }"></div></template><template v-if="item.type == 'top'"><div class="top" :style="{ background: isDark ? 'black' : '#fff' }"></div></template><template v-if="item.type == 'slide'"><div class="top"></div><div class="left" :style="{ background: isDark ? 'black' : '#fff' }"></div></template><i class="el-icon-check" v-show="checked == index"></i></div></div>
</template><script>
import { mapState } from 'vuex'
export default {props: {// 布局类型数组layouts: {type: Array,default: () => []}},data() {return {// 当前选中值checked: 0,}},computed: {// 获取是否是暗黑模式,从而缩略图实现暗黑效果...mapState(['isDark'])},methods: {// 切换选中值changeCheck(item, index) {this.checked = indexthis.$store.commit('set_layout_type', item.type)}}
}
</script><style lang="less" scoped>
.thumbnail {display: flex;width: 100%;.layout {position: relative;width: 50px;height: 50px;border: 1px solid gray;overflow: hidden;background: #f0f0f0;border-radius: 5px;cursor: pointer;.top {position: absolute;left: 0;top: 0;width: 100%;height: 25%;}.left {position: absolute;left: 0;top: 0;bottom: 0;width: 25%;height: 100%;}.el-icon-check {position: absolute;left: 50%;top: 50%;transform: translate(-50%, -50%);font-size: 20px;}}
}
</style>

(3)建立多个不同类型的布局文件: 

 

 侧边栏布局 :src/views/layout/SlideLayout.vue

<template><!-- 侧边栏布局 --><div><Sliderbar></Sliderbar><Header></Header><div class="content-box"><router-view /></div></div>
</template><script>
import Sliderbar from '@/components/Sliderbar.vue'
import Header from '@/components/Header.vue'
export default {components: {Header,Sliderbar,},
}
</script><style lang="less" scoped></style>

默认布局布局:src/views/layout/DefaultLayout.vue 

<template><!-- 默认布局 --><div><Header></Header><Sliderbar></Sliderbar><div class="content-box"><router-view /></div></div>
</template><script>
import Header from '@/components/Header.vue'
import Sliderbar from '@/components/Sliderbar.vue'
export default {components: { Header, Sliderbar },
}
</script><style lang="less" scoped></style>

顶部布局:src/views/layout/TopLayout.vue 

<template><!-- 顶栏布局 --><div><Header></Header><div class="content-box"><router-view /></div></div>
</template><script>
import Header from '@/components/Header.vue'
export default {components: {Header,},
}
</script><style lang="less" scoped></style>

  

 (4)首页组件 Home.vue,Home.vue下面渲染二级路由 

 

<template><!-- vuex获取选中的布局类型 --><div><defaultLayout v-show="layoutType == 'default'"></defaultLayout><slideLayout v-show="layoutType == 'slide'"></slideLayout><topLayout v-show="layoutType == 'top'"></topLayout></div>
</template><script>
import defaultLayout from './layout/DefaultLayout.vue'
import slideLayout from './layout/SlideLayout.vue'
import topLayout from './layout/TopLayout.vue'
import { mapState } from 'vuex'export default {components: { defaultLayout, slideLayout, topLayout },computed: {...mapState(['layoutType'])},
}
</script><style lang="less" scoped></style>

 (5)暗黑模式、布局类型变量都是保存在vuex中,因为多个组件之间进行数据通信比较方便!通过mapState取出vuex数据,然后通过computed接受mapState值,但如果想要直接修改mapState中的值则会报以下的错误:

         computed property "isDark" was assigned to but it has no setter.

        这是因为computed为只读的。不能直接修改computed的数据,要想修改则使用set

  computed: {...mapState(['isDark']),// computed property "isDark" was assigned to but it has no setter.  这是因为computed为只读的。不能直接修改computed的数据,要想修改则使用setdarkMode: {get() {return this.isDark},set(val) {this.$store.commit('set_is_dark', val)// 获取html根元素标签let html = document.documentElementif (val) {// html添加class="dark"选择器html.classList.add('dark')} else {// html移除class="dark"选择器html.classList.remove('dark')}}}},


http://www.ppmy.cn/news/51746.html

相关文章

MobTech MobPush|A/B测试提升运营决策

在实际推送过程中&#xff0c;我们常常有这样的困惑&#xff1a; 我们如何确定哪种推送内容更能吸引用户&#xff1f; 我们如何衡量推送效果的提升程度&#xff1f; 我们如何优化推送方案&#xff0c;实现更高的ROI&#xff1f; 为了解决这些困惑&#xff0c;我们需要一种科…

类加载和实例化

类初始化和实例初始化 一个实例对象的创建包括&#xff1a;类初始化和实例初始化1. 一个类要创建实例需要先加载并初始化该类,main方法所在的类需要先加载和初始化2. 一个子类要初始化需要先初始化父类3. 一个类初始化就是执行<clinit>()方法<clinit>方法由静态变量…

算法——分布式——一致性哈希、一致性hash图解动画

分布式算法——一致性哈希、一致性Hash 概述传统Hash算法算法步骤生成Hash环定位服务器定位数据和映射服务器 服务器变更Hash环倾斜虚拟节点总结 概述 一致性哈希算法在1997年由麻省理工学院提出&#xff0c;是一种特殊的哈希算法&#xff0c;目的是解决分布式缓存的问题。在移…

带你打开GCC的大门

START hi&#xff0c;大家好&#xff01; 今天带大家了解一下GCC。 首先说一句&#xff1a;大写的GCC和小写的gcc不是一个东西呦&#xff0c;下面我们慢慢道来... 1. GCC是什么&#xff1f; GNU Compiler Collection (GCC)是GNU项目开发的编译工具集&#xff0c;支持各种编…

DATAFAKER 使用方法记录

DATAFAKER 使用方法记录 win10 64位 Python 3.10.11 参考网址 datafaker的使用–详细教程 https://blog.csdn.net/A15517340610/article/details/105623103 https://github.com/gangly/datafaker python 版本 It is compatible with python2.7 and python3.4 也就是说 他…

Python 读写 CSV 数据

问题 你想读写一个 CSV 格式的文件 解决方案 对于大多数的 CSV 格式的数据读写问题&#xff0c;都可以使用 csv 库。 例如&#xff1a;假设你在一 个名叫 stocks.csv 文件中有一些股票市场数据&#xff0c;像这样&#xff1a; Symbol,Price,Date,Time,Change,Volume "…

为什么企业要做大规模敏捷?

背景 软件工程里一个重要的指标就是“可用的软件”&#xff0c;敏捷宣言里也同样告诉我们“工作的软件高于详尽的文档”&#xff0c;那“可用的软件”、“工作的软件”意味着什么呢&#xff1f;在我的理解里&#xff0c;可以经历用户 “千锤百炼”的软件就是一个“可用的软件”…

黑盒测试过程中【测试方法】详解4-因果图

在黑盒测试过程中&#xff0c;有9种常用的方法&#xff1a;1.等价类划分 2.边界值分析 3.判定表法 4.正交实验法 5.流程图分析 6.因果图法 7.输入域覆盖法 8.输出域覆盖法 9.猜错法 黑盒测试过程中【测试方法】讲解1-等价类&#xff0c;边界值&#xff0c;判定表_朝一…