一、前言
果然長時間坐着或站着,會給腰帶來很大負擔,聲明下我不是腰脫,就是個穿刺手術而已,身上有多處縫針沒長好,所以會給肚子和腰帶來一定的負擔。
上一篇文章已經寫了關於布局的開發,傳送門《Vue3學習(三)之網站首頁布局開發 》,但是我們寫代碼,肯定是繼承了優秀的代碼風格,封裝的特性,所以這里我們再對代碼進行修改,抽離公共部分的footer
和header
部分。
二、組件的開發
header
和footer
是公共的部分,每個頁面都有,所以要抽離出來,然后后續的維護再App.vue
中維護即可。
1、組件的構成
在components
下創建組件,基本結構如下:
由template
和script
兩對標簽構成
2、header部分組件的開發
如上圖紅圈部分所示,就是我們要進行抽離的公共部分,即組件的開發。
在components
下創建組件,header部分組件代碼如下:
<template>
<a-layout-header class="header">
<div class="logo" />
<a-menu
theme="dark"
mode="horizontal"
v-model:selectedKeys="selectedKeys1"
:style="{ lineHeight: '64px' }"
>
<a-menu-item key="1">nav 11122</a-menu-item>
<a-menu-item key="2">nav 2</a-menu-item>
<a-menu-item key="3">nav 3</a-menu-item>
</a-menu>
</a-layout-header>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
name: 'TheHeader',
});
</script>
3、footer組件的開發
如上圖所示,就是我們要footer部分組件的開發,示例代碼如下:
<template>
<a-layout-footer style="text-align: center">
軟件測試君 ©2021 Created by 六哥20211017
</a-layout-footer>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
name: 'TheFooter',
});
</script>
4、修改App.vue
示例代碼如下:
<template>
<a-layout>
<the-header></the-header>
<router-view/>
<the-footer></the-footer>
</a-layout>
</template>
<style>
#components-layout-demo-top-side-2 .logo {
float: left;
width: 120px;
height: 31px;
margin: 16px 24px 16px 0;
background: rgba(255, 255, 255, 0.3);
}
.ant-row-rtl #components-layout-demo-top-side-2 .logo {
float: right;
margin: 16px 0 16px 24px;
}
.site-layout-background {
background: #fff;
}
</style>
<script>
import TheHeader from "@/components/the-header";
import TheFooter from "@/components/the-footer";
export default {
components: {
TheHeader,
TheFooter
}
}
</script>
5、移除Helloword組件及相關代碼
home
修改如下:
<template>
<a-layout>
<a-layout-sider width="200" style="background: #fff">
<a-menu
mode="inline"
v-model:selectedKeys="selectedKeys2"
v-model:openKeys="openKeys"
:style="{ height: '100%', borderRight: 0 }"
>
<a-sub-menu key="sub1">
<template #title>
<span>
<user-outlined />
subnav 1
</span>
</template>
<a-menu-item key="1">option1</a-menu-item>
<a-menu-item key="2">option2</a-menu-item>
<a-menu-item key="3">option3</a-menu-item>
<a-menu-item key="4">option4</a-menu-item>
</a-sub-menu>
<a-sub-menu key="sub2">
<template #title>
<span>
<laptop-outlined />
subnav 2
</span>
</template>
<a-menu-item key="5">option5</a-menu-item>
<a-menu-item key="6">option6</a-menu-item>
<a-menu-item key="7">option7</a-menu-item>
<a-menu-item key="8">option8</a-menu-item>
</a-sub-menu>
<a-sub-menu key="sub3">
<template #title>
<span>
<notification-outlined />
subnav 3
</span>
</template>
<a-menu-item key="9">option9</a-menu-item>
<a-menu-item key="10">option10</a-menu-item>
<a-menu-item key="11">option11</a-menu-item>
<a-menu-item key="12">option12</a-menu-item>
</a-sub-menu>
</a-menu>
</a-layout-sider>
<a-layout-content
:style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }"
>
Content
</a-layout-content>
</a-layout>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
name: 'Home',
});
</script>
6、重啟服務查看
重新編譯,再次訪問頁面結果如下:
三、最后
雖然很坎坷,但是依舊堅持更新輸出,期望自己早點好起來,腰又開始不舒服了。。。。。。哎,真太難了。