【Vue 开发实战】实战篇 # 30:实现一个可动态改变的页面布局

简介: 【Vue 开发实战】实战篇 # 30:实现一个可动态改变的页面布局

说明

【Vue 开发实战】学习笔记。



基本布局

完成下面的一个布局,效果如下

31e893d409764a8cb63b9594423946b3.png


BasicLayout.vue

<template>
    <div :class="[`nav-theme-${navTheme}`, `nav-layout-${navLayout}`]">
        <a-layout id="components-layout-demo-side" style="min-height: 100vh">
            <a-layout-sider
                v-if="navLayout === 'left'"
                v-model="collapsed"
                :theme="navTheme"
                collapsible
                :trigger="null"
            >
                <div class="logo">Ant Design Vue Pro</div>
                <SiderMenu />
            </a-layout-sider>
            <a-layout>
                <a-layout-header style="background: #fff; padding: 0">
                    <a-icon
                        class="trigger"
                        :type="collapsed ? 'menu-unfold' : 'menu-fold'"
                        @click="collapsed = !collapsed"
                    ></a-icon>
                    <Header />
                </a-layout-header>
                <a-layout-content style="margin: 0 16px">
                    <router-view></router-view>
                </a-layout-content>
                <a-layout-footer style="text-align: center">
                    <Footer />
                </a-layout-footer>
            </a-layout>
        </a-layout>
        <SettingDrawer />
    </div>
</template>
<script>
import Header from "./Header";
import SiderMenu from "./SiderMenu";
import Footer from "./Footer";
import SettingDrawer from "../components/SettingDrawer";
export default {
    data() {
        return {
            collapsed: false,
        };
    },
    components: {
        Header,
        SiderMenu,
        Footer,
        SettingDrawer,
    },
    computed: {
        navTheme() {
            return this.$route.query.navTheme || "dark";
        },
        navLayout() {
            return this.$route.query.navLayout || "left";
        },
    },
};
</script>
<style lang="less" scoped>
.trigger {
    padding: 0 20px;
    line-height: 64px;
    font-size: 20px;
    &:hover {
        background-color: #eeeeee;
    }
}
.logo {
    height: 64px;
    line-height: 64px;
    text-align: center;
    overflow: hidden;
}
.nav-theme-dark {
    .logo {
        color: #fff;
    }
}
</style>



SettingDrawer 抽屉配置

<template>
    <div>
        <a-drawer
            width="300px"
            placement="right"
            :closable="false"
            :visible="visible"
            @close="onClose"
        >
            <template v-slot:handle>
                <div class="handle" @click="visible = !visible">
                    <a-icon :type="visible ? 'close' : 'setting'"></a-icon>
                </div>
            </template>
            <div>
                <h2>整体风格定制</h2>
                <a-radio-group
                    :value="$route.query.navTheme || 'dark'"
                    @change="(e) => handleSettingChange('navTheme', e.target.value)"
                >
                    <a-radio value="dark">黑色</a-radio>
                    <a-radio value="light">明亮</a-radio>
                </a-radio-group>
                <h2>导航模式</h2>
                <a-radio-group
                    :value="$route.query.navLayout || 'left'"
                    @change="(e) => handleSettingChange('navLayout', e.target.value)"
                >
                    <a-radio value="left">左侧</a-radio>
                    <a-radio value="top">顶部</a-radio>
                </a-radio-group>
            </div>
        </a-drawer>
    </div>
</template>
<script>
export default {
    data() {
        return {
            visible: false,
            navTheme: "dark",
            navLayout: "left"
        };
    },
    methods: {
        onClose() {
            this.visible = false;
        },
        handleSettingChange(type, value) {
            this.$router.push({
                query: {
                    ...this.$route.query,
                    [type]: value
                }
            });
        }
    },
};
</script>
<style scoped>
.handle {
    position: absolute;
    top: 240px;
    right: 300px;
    width: 48px;
    height: 48px;
    background-color: #1890ff;
    color: #fff;
    font-size: 20px;
    text-align: center;
    line-height: 48px;
    border-radius: 3px 0 0 3px;
}
</style>



Ant Design Vue helper

ant-design-vue-helper 是 Ant Design Vue 的 VS Code 扩展。可以自己安装 vscode 的扩展。

https://marketplace.visualstudio.com/items?itemName=ant-design-vue.vscode-ant-design-vue-helper


4194e447d7565f2a1b8fabbf50bbb116.gif



目录
相关文章
|
4天前
|
前端开发 JavaScript
Vue底层实现原理总结
Vue底层实现原理总结
8 0
|
6天前
|
JavaScript
|
7天前
|
存储 JavaScript API
Vue状态管理深度剖析:Vuex vs Pinia —— 从原理到实践的全面对比
Vue状态管理深度剖析:Vuex vs Pinia —— 从原理到实践的全面对比
13 2
|
3天前
|
JavaScript 前端开发
Vue躬行记(7)——渲染函数和JSX
Vue躬行记(7)——渲染函数和JSX
8 0
|
18天前
|
JavaScript 前端开发 开发者
vue3+ts配置跨域报错问题解决:> newpro2@0.1.0 serve > vue-cli-service serve ERROR Invalid options in vue.
【6月更文挑战第3天】在 Vue CLI 项目中遇到 &quot;ERROR Invalid options in vue.config.js: ‘server’ is not allowed&quot; 错误是因为尝试在 `vue.config.js` 中使用不被支持的 `server` 选项。正确配置开发服务器(如代理)应使用 `devServer` 对象,例如设置代理到 `http://xxx.com/`: ```javascript module.exports = { devServer: {
30 1
|
8天前
|
JavaScript 前端开发 测试技术
使用 Vue CLI 脚手架生成 Vue 项目
通过 Vue CLI 创建 Vue 项目可以极大地提高开发效率。它不仅提供了一整套标准化的项目结构,还集成了常用的开发工具和配置,使得开发者可以专注于业务逻辑的实现,而不需要花费大量时间在项目配置上。
67 7
使用 Vue CLI 脚手架生成 Vue 项目
|
10天前
|
JavaScript 算法
“Error: error:0308010C:digital envelope routines::unsupported”启动vue项目遇到一个错误【已解决
“Error: error:0308010C:digital envelope routines::unsupported”启动vue项目遇到一个错误【已解决
11 1
|
10天前
|
JavaScript
error Component name “Login“ should always be multi-word vue/multi-word-component-names【已解决】
error Component name “Login“ should always be multi-word vue/multi-word-component-names【已解决】
25 1
|
12天前
|
JavaScript API
【vue实战项目】通用管理系统:信息列表,信息录入
【vue实战项目】通用管理系统:信息列表,信息录入
19 3
|
12天前
|
JavaScript API
【vue实战项目】通用管理系统:信息列表,信息的编辑和删除
【vue实战项目】通用管理系统:信息列表,信息的编辑和删除
26 2