Vue+tsx使用slot沒(méi)有被替換的問(wèn)題
前言
最近自己準(zhǔn)備寫(xiě)一個(gè) UI
組件,想對(duì) vue
的 2.x
、3.x
可以更深層次的掌握
在架構(gòu)時(shí),準(zhǔn)備全部使用 tsx
書(shū)寫(xiě)組件
但遇到了 tsx
中使用 slot
的問(wèn)題
發(fā)現(xiàn)問(wèn)題
先寫(xiě)了一個(gè)基礎(chǔ)的 card
組件:
card.tsx:
import Component from 'vue-class-component' import VanUIComponent from '@packs/common/VanUIComponent' import { VNode } from 'vue' import { Prop } from 'vue-property-decorator' import { CardShadowEnum } from '@packs/config/card' @Component export default class Card extends VanUIComponent { @Prop({ type: String, default: undefined }) public headerPadding !: string | undefined @Prop({ type: String, default: '' }) public title !: string @Prop({ type: String, default: CardShadowEnum.Hover }) public shadow !: CardShadowEnum public static componentName = 'v-card' public get wrapperClassName(): string { const list: string[] = ['v-card__wrapper'] list.push(`shadow-${ this.shadow }`) return list.join(' ') } public render(): VNode { return ( <div class={ this.wrapperClassName }> <div class="v-card__header" style={ { padding: this.headerPadding } }> { this.$slots.title ? <slot name="title" /> : <div>{ this.title }</div> } </div> <div class="v-card__body"> <slot name="default" /> </div> <div class="v-card__footer"></div> </div> ) } }
在 examples
中使用這個(gè) v-card
的時(shí)候:
<template> <v-card> <template #title>1111</template> </v-card> </template> <script lang="ts"> import Vue from 'vue' import Component from 'vue-class-component' @Component export default class Components extends Vue { } </script> <style lang="scss" scoped> .components__wrapper { padding: 20px; } </style>
我發(fā)現(xiàn)渲染后,瀏覽器不替換 slot
標(biāo)簽:
我在百度、Google尋找了一天也沒(méi)有解釋,在官方文檔中仔仔細(xì)細(xì)閱讀后,也沒(méi)有類似的提示,以及 jsx
編譯器的文檔中也沒(méi)有寫(xiě)明,只是聲明了如何使用命名 slot
。
解決
第二天,我一直在糾結(jié)這個(gè),也查了 element-ui
、ant-design-vue
的 UI
組件庫(kù)中如何書(shū)寫(xiě),也沒(méi)有找到對(duì)應(yīng)的使用 jsx
使用 slot
的。
不甘放棄的我更換了搜索文字,于是終于找到解決方案,并將代碼改為:
... public render(): VNode { return ( <div class={ this.wrapperClassName }> <div class="v-card__header" style={ { padding: this.headerPadding } }> { this.$slots.title ?? <div>{ this.title }</div> } </div> <div class="v-card__body"> <slot name="default" /> </div> <div class="v-card__footer"></div> </div> ) } ...
再查看瀏覽器渲染:
問(wèn)題解決
后記
在使用 jsx / tsx
時(shí),如果 js
語(yǔ)法本身可以解決的,或本身注冊(cè)在 this
上的方法,優(yōu)先使用 js
去做,例如 v-if / v-else
可以使用 雙目運(yùn)算符
替代。實(shí)在沒(méi)有可用的簡(jiǎn)便方法,再使用 vue
的指令做,例如 v-show
。
到此這篇關(guān)于Vue+tsx使用slot沒(méi)有被替換的問(wèn)題的文章就介紹到這了,更多相關(guān)Vue+tsx slot沒(méi)有被替換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue開(kāi)發(fā)項(xiàng)目中如何使用Font Awesome 5
Font Awesome是一套流行的圖標(biāo)字體庫(kù),我們?cè)趯?shí)際開(kāi)發(fā)的過(guò)程中會(huì)經(jīng)常遇到需要使用圖標(biāo)的場(chǎng)景,對(duì)于一些常用的圖標(biāo),我們可以直接在Font Awesome中找到并且使用,這篇文章主要給大家介紹了關(guān)于Vue開(kāi)發(fā)項(xiàng)目中如何使用Font Awesome5的相關(guān)資料,需要的朋友可以參考下2021-11-11vue2.0中自適應(yīng)echarts圖表、全屏插件screenfull的使用
這篇文章主要介紹了vue2.0中自適應(yīng)echarts圖表、全屏插件screenfull的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08vue3編譯報(bào)錯(cuò)ESLint:defineProps is not defined&nbs
這篇文章主要介紹了vue3編譯報(bào)錯(cuò)ESLint:defineProps is not defined no-undef的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03vue swipe自定義組件實(shí)現(xiàn)輪播效果
這篇文章主要為大家詳細(xì)介紹了vue swipe自定義組件實(shí)現(xiàn)輪播效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07解決vue的router組件component在import時(shí)不能使用變量問(wèn)題
這篇文章主要介紹了解決vue的router組件component在import時(shí)不能使用變量問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07Vue中通過(guò)vue-router實(shí)現(xiàn)命名視圖的問(wèn)題
這篇文章主要介紹了在Vue中通過(guò)vue-router實(shí)現(xiàn)命名視圖,本文給大家提到了vue-router的原理解析,給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04vue實(shí)現(xiàn)登錄、注冊(cè)、退出、跳轉(zhuǎn)等功能
這篇文章主要介紹了vue實(shí)現(xiàn)登錄、注冊(cè)、退出、跳轉(zhuǎn)等功能,需要的朋友可以參考下2020-12-12詳解Vue.js在頁(yè)面加載時(shí)執(zhí)行某個(gè)方法
這篇文章主要介紹了詳解Vue.js在頁(yè)面加載時(shí)執(zhí)行某個(gè)方法的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11