溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Vue如何實(shí)現(xiàn)插槽下渲染dom字符串使用

發(fā)布時(shí)間:2023-04-17 16:00:44 來源:億速云 閱讀:108 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“Vue如何實(shí)現(xiàn)插槽下渲染dom字符串使用”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

需求

先來簡單介紹下需求:這是在開發(fā)一個(gè)低代碼平臺的時(shí)候所遇到的需求,用戶可以自己寫一些組件上傳到平臺,在使用的時(shí)候可以對組件的 props slots events 進(jìn)行配置,這就涉及到了動態(tài)插槽內(nèi)容的實(shí)現(xiàn)了。對于代碼編輯器的實(shí)現(xiàn)使用了 code-mirror,感興趣的可以去看下,這里就不多說了。這里主要講如何實(shí)現(xiàn)動態(tài)插槽內(nèi)容渲染。

先來大致看下代碼的上下文:

<template>
    <Component
        v-bind="componentProps"
    >
        <template
            v-for="item of componentSlots"
            #[item[0]]
        >
            
        </template>
    </Component>
</template>

<script setup lang="ts">
    const Component = defineAsyncComponent({
        // ...
    })
    const componentProps = ref({})
    const componentSlots = ref<[string, string][]>([])
    
    onMounted(async () => {
        componentProps.value = await loadProps()
        componentSlots.value = await loadSlots()
    })
</script>

v-html

說到渲染 dom 字符串,那 v-html 肯定是首要想到的。但是 template 標(biāo)簽上是無法使用 v-html 的,那么只能在 template 下寫一個(gè)普通元素來塞 dom 字符串,代碼如下:

<template
    v-for="item of componentSlots"
    #[item[0]]
>
    <span v-html="item[1]">
    </span>
</template>

這樣的確實(shí)現(xiàn)了動態(tài)渲染插槽內(nèi)容的需求,但是多出一個(gè)標(biāo)簽總是感覺不太妥當(dāng);而且也很難保證這個(gè)多出的 span 不會對組件的布局產(chǎn)生影響。這讓我又陷入的沉思...

v-outerHTML

既然 innerHTML 不完全滿足需求,那么使用 outerHTML 不就完美解決這個(gè)問題了嗎?于是我去查關(guān)于 vue 如何使用 outerHTML 相關(guān)資料,發(fā)現(xiàn)并沒有很好的案例,那就自己實(shí)現(xiàn)一個(gè)吧。

export const vOuterHTML = {
    bind(el, binding) {
        el.outerHTML = binding.value
    },
    update(el, binding) {
        el.outerHTML = binding.value
    },
}
<template
    v-for="item of componentSlots"
    #[item[0]]
>
    <span v-outerHTML="item[1]">
    </span>
</template>

代碼保存,頁面一刷新,這不完美實(shí)現(xiàn)了嗎?但是等我去編輯 dom 字符串并保存后發(fā)現(xiàn)問題了,組件渲染內(nèi)容并沒有改變,控制臺也報(bào)錯(cuò)了。

什么原因呢?原來是因?yàn)樵?update 階段時(shí),span 已不在頁面中,因此無法對他執(zhí)行 outerHTML 賦值。

那怎么辦呢?只需要在 bind 階段記住 span 的父節(jié)點(diǎn),然后在更新階段手動再創(chuàng)建一個(gè) spanappend 到父節(jié)點(diǎn)下,再進(jìn)行 outerHTML賦值即可,代碼如下:

export const vOuterHTML = (() => {
    let parentNode = null
    
    return {
        bind(el, binding) {
            parentNode = el.parentNode
            el.outerHTML = binding.value
        },
        update(el, binding) {
            if (parentNode) {
                const span = document.createElement('span')
                parentNode.appendChild(span)
                span.outerHTML = binding.value
            }
        },
        unbind() {
            if (parentNode) {
                parentNode = null
            }
        }
    }
})()

“Vue如何實(shí)現(xiàn)插槽下渲染dom字符串使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI