Vue+tsx使用slot沒有被替換的問題
前言
最近自己準(zhǔn)備寫一個(gè) UI 組件,想對 vue 的 2.x、3.x 可以更深層次的掌握
在架構(gòu)時(shí),準(zhǔn)備全部使用 tsx 書寫組件
但遇到了 tsx 中使用 slot 的問題
發(fā)現(xiàn)問題
先寫了一個(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尋找了一天也沒有解釋,在官方文檔中仔仔細(xì)細(xì)閱讀后,也沒有類似的提示,以及 jsx 編譯器的文檔中也沒有寫明,只是聲明了如何使用命名 slot。
解決
第二天,我一直在糾結(jié)這個(gè),也查了 element-ui 、ant-design-vue 的 UI 組件庫中如何書寫,也沒有找到對應(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>
)
}
...
再查看瀏覽器渲染:

問題解決
后記
在使用 jsx / tsx 時(shí),如果 js 語法本身可以解決的,或本身注冊在 this 上的方法,優(yōu)先使用 js 去做,例如 v-if / v-else 可以使用 雙目運(yùn)算符 替代。實(shí)在沒有可用的簡便方法,再使用 vue 的指令做,例如 v-show 。
到此這篇關(guān)于Vue+tsx使用slot沒有被替換的問題的文章就介紹到這了,更多相關(guān)Vue+tsx slot沒有被替換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue路由跳轉(zhuǎn)攜帶參數(shù)的方式總結(jié)
我們知道在vue中每個(gè)頁面都需要在路由中聲明,下面這篇文章主要給大家介紹了關(guān)于vue路由跳轉(zhuǎn)攜帶參數(shù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10
vue嵌套路由與404重定向?qū)崿F(xiàn)方法分析
這篇文章主要介紹了vue嵌套路由與404重定向?qū)崿F(xiàn)方法,結(jié)合實(shí)例形式分析了vue.js嵌套路由與404重定向的概念、原理、實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
使用babel-plugin-import?實(shí)現(xiàn)自動按需引入方式
這篇文章主要介紹了使用babel-plugin-import?實(shí)現(xiàn)自動按需引入方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
vue-router 控制路由權(quán)限的實(shí)現(xiàn)
這篇文章主要介紹了vue-router 控制路由權(quán)限的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

