Vue3 組件的開發(fā)詳情
一、前言
果然長時間坐著或站著,會給腰帶來很大負(fù)擔(dān),聲明下我不是腰脫,就是個穿刺手術(shù)而已,身上有多處縫針沒長好,所以會給肚子和腰帶來一定的負(fù)擔(dān)。
上一篇文章已經(jīng)寫了關(guān)于布局的開發(fā),傳送門《Vue3(三)網(wǎng)站首頁布局開發(fā) 》,但是我們寫代碼,肯定是繼承了優(yōu)秀的代碼風(fēng)格,封裝的特性,所以這里我們再對代碼進(jìn)行修改,抽離公共部分的footer和header部分。
二、組件的開發(fā)
header和footer是公共的部分,每個頁面都有,所以要抽離出來,然后后續(xù)的維護(hù)再App.vue中維護(hù)即可。
1、組件的構(gòu)成
在components下創(chuàng)建組件,基本結(jié)構(gòu)如下:

由template和script兩對標(biāo)簽構(gòu)成
2、header部分組件的開發(fā)

如上圖紅圈部分所示,就是我們要進(jìn)行抽離的公共部分,即組件的開發(fā)。
在components下創(chuàng)建組件,header部分組件代碼如下:
html
<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組件的開發(fā)

如上圖所示,就是我們要footer部分組件的開發(fā),示例代碼如下:
html
<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
示例代碼如下:
html
<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組件及相關(guān)代碼
home修改如下:
html
<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、重啟服務(wù)查看
重新編譯,再次訪問頁面結(jié)果如下:

三、最后
到此這篇關(guān)于Vue3(四) 組件的開發(fā)的文章就介紹到這了,更多相關(guān)Vue3 組件開發(fā)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3 組件間通信之mitt實(shí)現(xiàn)任意組件間通信的步驟
mitt 主要有4個API:emit(觸發(fā)某個事件)、on(綁定事件)、off(解綁某個事件)、all(獲取所有綁定的事件),這篇文章主要介紹了Vue3 組件間通信之mitt實(shí)現(xiàn)任意組件間通信,需要的朋友可以參考下2024-05-05
Vue2.0父組件與子組件之間的事件發(fā)射與接收實(shí)例代碼
這篇文章主要介紹了Vue2.0父組件與子組件之間的事件發(fā)射與接收實(shí)例代碼,需要的朋友可以參考下2017-09-09
關(guān)于vue3?解決getCurrentInstance?打包后線上環(huán)境報(bào)錯問題
這篇文章主要介紹了vue3?解決getCurrentInstance?打包后線上環(huán)境報(bào)錯問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
利用WebStorm創(chuàng)建一個Vue項(xiàng)目的完整步驟
WebStorm是一個非常適合學(xué)習(xí)和開發(fā)Vue項(xiàng)目的集成開發(fā)環(huán)境,下面這篇文章主要給大家介紹了關(guān)于利用WebStorm創(chuàng)建一個Vue項(xiàng)目的完整步驟,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06
vue子組件封裝彈框只能執(zhí)行一次的mounted問題及解決
這篇文章主要介紹了vue子組件封裝彈框只能執(zhí)行一次的mounted問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
vue?select組件綁定的值為數(shù)字類型遇到的問題
這篇文章主要介紹了vue?select組件綁定的值為數(shù)字類型遇到的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
詳解vue中在父組件點(diǎn)擊按鈕觸發(fā)子組件的事件
這篇文章主要介紹了詳解vue中在父組件點(diǎn)擊按鈕觸發(fā)子組件的事件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

