html可以放到slot中间
示例
BaseCard.vue(新建slot)
<template>
<div>
<slot></slot>
</div>
</template>
<script>
export default {
};
</script>
<style scoped>
div {
margin: 2rem auto;
max-width: 30rem;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.26);
padding: 1rem;
}
</style>
UserInfo.vue(使用slot)
<template>
<section>
<base-card>
<div>
<h3>{{ fullName }}</h3>
<base-badge :type="role" :caption="role.toUpperCase()"></base-badge>
</div>
<p>{{ infoText }}</p>
</base-card>
</section>
</template>
<script>
export default {
props: ['fullName', 'infoText', 'role'],
};
</script>
<style>
section {
margin: 2rem auto;
max-width: 30rem;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.26);
padding: 1rem;
}
section div {
display: flex;
justify-content: space-between;
align-items: center;
}
</style>
main.js
import { createApp } from 'vue';
import App from './App.vue';
import BaseBadge from './components/BaseBadge.vue';
import BaseCard from './components/BaseCard.vue';
const app = createApp(App);
app.component('base-badge', BaseBadge);
app.component('base-card', BaseCard);
app.mount('#app');