gpt4 book ai didi

vue.js - 获取 Vue 插槽的渲染 HTML 内容作为变量

转载 作者:搜寻专家 更新时间:2023-10-30 22:37:34 25 4
gpt4 key购买 nike

我知道你可以这样做:

<div id="app">
<div class="container">
<h1 class="title is-1" v-html="text"></h1>
<get-slot-contents>
<p>Hi there</p>
<p>Hi <span>heloo</span></p>
<p>Hi there</p>
<p>Hi there</p>
</get-slot-contents>
</div>
</div>

// JS

Vue.component('get-slot-contents', {
data: function () {
return {
html : false
}
},
template: `<div>
****
<slot></slot>
****
</div>`,
mounted(){
console.log(this.$slots.default);
}
});

这会记录以下内容:

[fo, fo, fo, fo, fo, fo, fo]

每个 fo 包含如下内容:

{tag: undefined, data: undefined, children: undefined, text: " ", elm: text, …}

我如何 console.log 像这样:

<p>Hi there</p><p>Hi <span>heloo</span></p><p>Hi there</p><p>Hi there</p>

出于此目的,您可以假设没有嵌套组件。

最佳答案

this.$el 获取 innerHTML 对你有用吗?

演示:https://codepen.io/jacobgoh101/pen/vjmzWg?editors=1010

<div id="app">
<div class="container">
<h1 class="title is-1"></h1>
<get-slot-contents>
<p>Hi there</p>
<p>Hi <span>heloo</span></p>
<p>Hi there</p>
<p>Hi there</p>
</get-slot-contents>
</div>
</div>
Vue.component("get-slot-contents", {
data: function() {
return {
html: false
};
},
template: `<div>
****
<div class="slot-wrapper">
<slot></slot>
</div>
****
</div>`,
mounted() {
console.log(this.$el.getElementsByClassName("slot-wrapper")[0].innerHTML);
}
});

new Vue({
el: "#app"
});

关于vue.js - 获取 Vue 插槽的渲染 HTML 内容作为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50133153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com