【问题标题】:Using Vue.js 3 component dynamically in the D3.js v7在 D3.js v7 中动态使用 Vue.js 3 组件
【发布时间】:2021-10-04 10:43:11
【问题描述】:

我使用Vue.js 3 和D3.js v7 制作流程图。

我的问题是我无法在 D3.js 中动态附加组件。

我的组件被导入如下所示。

components: {
    StoryPanel: defineAsyncComponent(() => import("@/Pages/Story/Partials/StoryPanel"))
},
let nodes = container.selectAll("g")
    .data(root.descendants())
    .join("g")

    .append("foreignObject")
    .attr("width", entityWidth - 10)
    .attr("height", 150)

nodes.append("xhtml:div")
    .attr("class", "border border-black")
    .html(function (d) {
        // return '<StoryPanel />' tried this but not working
    })
    .style("border", '1px solid black')

这是生成的html

<foreignObject width="190" height="150" transform="translate(-95,10)">
    <div class="border border-black" style="border: 1px solid black;">
        <storypanel></storypanel>
    </div>
</foreignObject>

[编辑 1] 按照@MichalLevý 的建议尝试了Rendering Vue 3 component into HTML string,但仍然无法正常工作

.html(function (d) {
    const tempApp = createApp({
        render() {
            return h(StoryPanel,{step: d})
        }
    })

    const el = document.createElement('div');
    const mountedApp = tempApp.mount(el)

    return mountedApp.$el.outerHTML
})

[编辑 2] 我发现它仅在使用 const 而不是 Component.vue

时才有效
const CircleWithTextSvg = {
    name: 'CircleWithTextSvg',
    props: {
        text: {
            type: String,
            default: "1"
        },
        fill: {
            type: String,
            default: "white"
        }
    },
    template: `<div>template</div>`
}

感谢任何帮助。谢谢。

【问题讨论】:

标签: d3.js vuejs3


【解决方案1】:

使用createApp

import {createApp} from 'vue';

改用它并返回outerHTML

const app = createApp(Component, {prop1: this.prop1})
    .mount(document.createElement('div'))
return app.$el.outerHTML

【讨论】:

    猜你喜欢
    • 2021-09-24
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    相关资源
    最近更新 更多