Vue3中同時定義多個插槽的實現(xiàn)示例
概述
當(dāng)你想要給外部預(yù)留多個位置的時候,具名插槽就非常有用了。
比如,我們定義一個卡片,讓別人使用的時候,標(biāo)題可以自定義,內(nèi)容也可以自定義,這個時候就需要兩個插槽。
基本用法
我們創(chuàng)建src/components/Demo32.vue,代碼如下:
<template>
<div>
<slot name="title">
<h3>卡片默認(rèn)標(biāo)題</h3>
</slot>
<slot name="content">
<div>卡片默認(rèn)內(nèi)容</div>
</slot>
</div>
</template>
接著,我們修改src/App.vue:
<script setup>
import Demo from "./components/Demo32.vue"
</script>
<template>
<h1>歡迎跟著Python私教一起學(xué)習(xí)Vue3入門課程</h1>
<hr>
<demo/>
<hr>
<demo>
<template #title>
<h3>自定義的標(biāo)題</h3>
</template>
<template #content>
<div>自定義的內(nèi)容</div>
</template>
</demo>
</template>
然后,我們?yōu)g覽器訪問:http://localhost:5173/

完整代碼
package.json
{
"name": "hello",
"private": true,
"version": "0.1.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build"
},
"dependencies": {
"vue": "^3.3.8"
},
"devDependencies": {
"@vitejs/plugin-vue": "^4.5.0",
"vite": "^5.0.0"
}
}
vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig({
plugins: [vue()],
})
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" rel="external nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Vue</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>
src/main.js
import { createApp } from 'vue'
import App from './App.vue'
createApp(App).mount('#app')
src/App.vue
<script setup>
import Demo from "./components/Demo32.vue"
</script>
<template>
<h1>歡迎跟著Python私教一起學(xué)習(xí)Vue3入門課程</h1>
<hr>
<demo/>
<hr>
<demo>
<template #title>
<h3>自定義的標(biāo)題</h3>
</template>
<template #content>
<div>自定義的內(nèi)容</div>
</template>
</demo>
</template>
src/components/Demo32.vue
<template>
<div>
<slot name="title">
<h3>卡片默認(rèn)標(biāo)題</h3>
</slot>
<slot name="content">
<div>卡片默認(rèn)內(nèi)容</div>
</slot>
</div>
</template>
啟動方式
yarn yarn dev
瀏覽器訪問:http://localhost:5173/
到此這篇關(guān)于Vue3中同時定義多個插槽的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)Vue3同時定義多個插槽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue mint-ui學(xué)習(xí)筆記之picker的使用
本篇文章主要介紹了vue mint-ui學(xué)習(xí)筆記之picker的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
vue3.0 proxy設(shè)置代理不成功的問題及解決方案
這篇文章主要介紹了vue3.0 proxy設(shè)置代理不成功的問題及解決方案,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-12-12
vue等框架對Tabs、Moda等設(shè)置固定高度后沒有滾動條問題
這篇文章主要介紹了vue等框架對Tabs、Moda等設(shè)置固定高度后沒有滾動條問題,解決方法很簡單,只需要一行簡短代碼就可以解決,下面小編給大家詳細講解,需要的朋友可以參考下2023-05-05
vue中 el-table每個單元格包含多個數(shù)據(jù)項處理
vue項目中,我們需要在el-table中顯示數(shù)組數(shù)據(jù),有的時候,需要在一個單元格中顯示多條數(shù)據(jù),如何實現(xiàn)呢,對vue el-table單元格相關(guān)知識感興趣的朋友一起看看吧2023-11-11
vue刷新子組件、重置組件以及重新加載子組件項目實戰(zhàn)記錄
在vue開發(fā)中出于各種目的,我們常常需要讓組件重新加載渲染,這篇文章主要給大家介紹了關(guān)于vue刷新子組件、重置組件以及重新加載子組件的相關(guān)資料,需要的朋友可以參考下2023-12-12
Vue Router中獲取路由傳遞過來的參數(shù)(方法詳解)
在VueRouter中,可以通過動態(tài)路由匹配和查詢參數(shù)`query`來傳遞參數(shù),并將路由參數(shù)或查詢參數(shù)作為組件的`props`傳遞,動態(tài)路由匹配使用`route.params`訪問參數(shù),查詢參數(shù)使用`route.query`訪問,本文給大家介紹Vue Router中獲取路由傳遞過來的參數(shù),感興趣的朋友一起看看吧2025-02-02

