1)在組件目錄中創建公用組件文件,comTitle.vue如下:
//comTitle.vue <template> <div class="title-common"> <div class="title"> <span :class="[titleSize]" v-text="title"/> <span class="title-sub" v-if="subTitle" v-text="subTitle"/> </div> <slot></slot> </div> </template> <script> export default { props: { size: { type: String, default: "normal" }, //標題 title: { type: String, required: true }, //小標題說明 subTitle: { type: String, default: "" } }, computed: { titleSize() { return `title-${this.size}`; } } }; </script> <style lang="postcss" scoped> @import url("common.postcss"); .title-common { display: flex; align-items: center; justify-content: space-between; & .title { display: flex; align-items: center; } & .title-sub { font-size: var(--h6); color: #363636; padding-left: 19px; } & .title > span:first-child { color: #000; border-left: 4px solid var(--primaryBlueColor); padding-left: 9px; } & .title-big { display: inline-block; font-size: 18px; line-height: 18px; font-weight: 700; } & .title-normal { font-size: var(--h3); line-height: var(--h3); } & .title-small { font-size: var(--h3); line-height: var(--h3); font-weight: 550; } & .title-mini { font-size: var(--h4); line-height: var(--h4); font-weight: 550; } & .title-exmini { font-size: var(--h6); line-height: var(--h6); font-weight: 550; } } </style>
2)在業務組件中使用公用組件,testComponent.vue如下:
<template> <div> <com-title title="標題" size="small"></com-title> </div> </template> <script> import comTitle from "components/comTitle";//引入 export default { name:'testComponent', components:{ comTitle } } </script>