【发布时间】:2021-08-19 11:54:59
【问题描述】:
TL;DR:
将骨干网/jquery 应用程序转换为 Vue 3。拥有超过 400 个用于不同视图的 html 模板文件。希望能够以原始格式动态解析这些 html 模板,而无需进行大量重写。还希望能够在 html 中添加对 Vue 组件的模板引用并让 Vue 解析。
原始问题
如何在 Vue 组件中定义外部或动态 HTML 模板(如 MyVueTemplate.html)?
类似(伪代码):
export default {
name: "MyComponent",
template: import(`@/views/${this.html_id}.html`),
props: {
html_id: String,
},
components: {
}
};
一旦成功,我需要能够让 temp.html 包含 Vue 组件的标签,例如 <MyOtherViewComponent variable1="test"></MyOtherViewComponent>,然后由 Vue 解析。
我希望 webpack import() 语句的使用方式与我们当前导入 html 模板的方式相同,但也可以在构建期间解析 Vue 组件引用。但如果需要,运行时编译也可能是一个(不太受欢迎的)选项。
有超过 400 个 html 文件,因此目前手动将它们全部重构为 Vue 组件是不可行的,因此这种方法。使用 Vue 3,但即使是 Vue 2 的解决方案也可以作为更多研究的起点。
希望有人对这个问题有所了解。
更新
到目前为止,在旧 html 中包含 Vue 组件的唯一方法似乎是在 html 中包含标签,并使用 createApp().mount() 为每个组件创建单独的 Vue 实例.不理想,但可能是唯一的方法。
【问题讨论】:
-
我做了一些搜索......也许你可以在这里找到一些想法:medium.com/scrumpy/…
-
@redfox05 你能和
v-html一起工作吗? -
@PeterKrebs 谢谢,但遗憾的是我已经尝试过了,它适用于动态加载的组件,但我需要源文件是纯 html 的东西,因为这是我们 400 多个模板的格式:( wittgenstein谢谢,但遗憾的是 v-html 在这里不起作用。
"Note that the contents are inserted as plain HTML - they will not be compiled as Vue templates."
标签: vue.js webpack vue-component vuejs3 webpack-4