Vue多布局模式實(shí)現(xiàn)方法詳細(xì)講解
1、目標(biāo)效果
源碼地址:multipal-layout-demo: vue2實(shí)現(xiàn)多布局+暗黑模式
默認(rèn)布局:頭部寬度100%,側(cè)邊欄、內(nèi)容區(qū)

頂部布局:頭部寬度100%,內(nèi)容區(qū)

側(cè)邊欄布局:側(cè)邊欄高度100%,頭部、內(nèi)容區(qū)

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)布局縮略圖如何實(shí)現(xiàn)?用div + css 手動(dòng)實(shí)現(xiàn)布局樣式
父組件傳遞一個(gè)布局類型數(shù)組,遍歷此組件;用一個(gè)變量保存索引值,點(diǎn)擊不同的布局類型項(xiàng)時(shí)切換索引并在vuex修改當(dāng)前選中的布局類型
將縮略圖封裝成組件: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: {
// 布局類型數(shù)組
layouts: {
type: Array,
default: () => []
}
},
data() {
return {
// 當(dāng)前選中值
checked: 0,
}
},
computed: {
// 獲取是否是暗黑模式,從而縮略圖實(shí)現(xiàn)暗黑效果
...mapState(['isDark'])
},
methods: {
// 切換選中值
changeCheck(item, index) {
this.checked = index
this.$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)建立多個(gè)不同類型的布局文件:
側(cè)邊欄布局 :src/views/layout/SlideLayout.vue
<template>
<!-- 側(cè)邊欄布局 -->
<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>默認(rèn)布局布局:src/views/layout/DefaultLayout.vue
<template>
<!-- 默認(rèn)布局 -->
<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下面渲染二級(jí)路由
<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中,因?yàn)槎鄠€(gè)組件之間進(jìn)行數(shù)據(jù)通信比較方便!通過mapState取出vuex數(shù)據(jù),然后通過computed接受mapState值,但如果想要直接修改mapState中的值則會(huì)報(bào)以下的錯(cuò)誤:
computed property "isDark" was assigned to but it has no setter.
這是因?yàn)閏omputed為只讀的。不能直接修改computed的數(shù)據(jù),要想修改則使用set
computed: {
...mapState(['isDark']),
// computed property "isDark" was assigned to but it has no setter. 這是因?yàn)閏omputed為只讀的。不能直接修改computed的數(shù)據(jù),要想修改則使用set
darkMode: {
get() {
return this.isDark
},
set(val) {
this.$store.commit('set_is_dark', val)
// 獲取html根元素標(biāo)簽
let html = document.documentElement
if (val) {
// html添加class="dark"選擇器
html.classList.add('dark')
} else {
// html移除class="dark"選擇器
html.classList.remove('dark')
}
}
}
},到此這篇關(guān)于Vue多布局模式實(shí)現(xiàn)方法詳細(xì)講解的文章就介紹到這了,更多相關(guān)Vue多布局模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue 使用html2canvas將DOM轉(zhuǎn)化為圖片的方法
這篇文章主要介紹了vue 使用html2canvas將DOM轉(zhuǎn)化為圖片的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09
vue+element獲取el-table某行的下標(biāo),根據(jù)下標(biāo)操作數(shù)組對(duì)象方式
這篇文章主要介紹了vue+element獲取el-table某行的下標(biāo),根據(jù)下標(biāo)操作數(shù)組對(duì)象方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨想過來看看吧2020-08-08
Vue中foreach數(shù)組與js中遍歷數(shù)組的寫法說明
這篇文章主要介紹了Vue中foreach數(shù)組與js中遍歷數(shù)組的寫法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
vue+element項(xiàng)目中過濾輸入框特殊字符小結(jié)
這篇文章主要介紹了vue+element項(xiàng)目中過濾輸入框特殊字符小結(jié),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
jdk1.8+vue elementui實(shí)現(xiàn)多級(jí)菜單功能
這篇文章主要介紹了jdk1.8+vue elementui實(shí)現(xiàn)多級(jí)菜單功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
基于Vue技術(shù)實(shí)現(xiàn)遞歸組件的方法
這篇文章主要為大家詳細(xì)介紹了基于Vue技術(shù)實(shí)現(xiàn)遞歸組件的方法 ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
laravel5.3 vue 實(shí)現(xiàn)收藏夾功能實(shí)例詳解
這篇文章主要介紹了laravel5.3 vue 實(shí)現(xiàn)收藏夾功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-01-01
詳解element-ui日期時(shí)間選擇器的日期格式化問題
這篇文章主要介紹了詳解element-ui日期時(shí)間選擇器的日期格式化問題,本文用到了DateTimePicker來選擇日期時(shí)間,但是在將數(shù)據(jù)傳回后臺(tái)的過程中遇到了一些令人頭疼的問題,有興趣的一起來了解一下2019-04-04

