【发布时间】:2022-10-02 01:36:39
【问题描述】:
我正在尝试使用 Vue 3 创建一个 gridstack.js 仪表板,并且我希望网格堆栈项包含反应式 Vue 3 组件。
问题是这些网格堆栈项只能传递 HTML。文档说明您应该能够将 Vue 组件添加为内容,但这些示例适用于 Vue 2,我正在努力在 Vue 3 中实现这一点。
我有以下代码:
<template>
<div class=\"p-6 h-full flex flex-col\">
<header class=\"flex flex-row items-center justify-between mb-6\">
<div>
<h1 class=\"text-3xl font-bold\">
Workbench
</h1>
<p class=\"leading-6 text-gray-600 text-sm mt-2\">
{{ info }}
</p>
</div>
<div class=\"flex flex-row items-center\">
<button type=\"button\" @click=\"addPanel()\">Add Panel</button>
</div>
</header>
<div class=\"flex-1\">
<section class=\"grid-stack\"></section>
</div>
</div>
</template>
<script setup>
import { ref, onMounted, defineComponent, createApp } from \"vue\"
import TestPanel from \"./../components/panels/TestPanel.vue\"
let grid = null;
const items = [
{ x: 0, y: 0, h: 4, w: 6 },
{ x: 7, y: 0, h: 4, w: 6 },
{ x: 0, y: 5, h: 4, w: 4 },
{ x: 4, y: 5, h: 4, w: 4 },
{ x: 8, y: 5, h: 4, w: 4 },
];
onMounted(() => {
grid = GridStack.init({
// float: true,
cellHeight: \"70px\",
minRow: 1,
});
grid.load(items)
});
function addPanel() {
const div = document.createElement(\"div\")
div.id = Math.random().toString(24).substring(8)
const componentInstance = defineComponent({
extends: TestPanel, data() {
return {
test: \"this is a test\"
}
}
})
const app = createApp(componentInstance)
app.mount(div)
let widget = grid.addWidget({
x: 0,
y: 0,
w: 6,
h: 3,
content: div.outerHTML,
})
app.mount(div.id)
}
</script>
<style>
.grid-stack-item-content {
background-color: #18BC9C;
}
</style>
这会将 vue 组件加载到堆栈网格项中,但该组件不再是反应式的。
任何帮助将不胜感激,在此先感谢!
标签: javascript vue.js vuejs3