简体   繁体   English

在 Ajax 请求之后以动态附加的 HTML 呈现组件

[英]Render component in dynamically appended HTML after Ajax Request

Use case:用例:

I have the dependency of a JQuery Library which appends HTML dinamically after some Ajax requests.我有一个 JQuery 库的依赖项,它在一些 Ajax 请求之后动态地附加 HTML。 I would like to display Vue components inside the appended HTML.我想在附加的 HTML 中显示 Vue 组件。 I can supply the content of the HTML, but I can't change the way it gets appended to the DOM using JQuery.我可以提供 HTML 的内容,但我无法更改它使用 JQuery 附加到 DOM 的方式。

I have no choice but to render the component when the Ajax request finnishes and the HTML was appended to the DOM.我别无选择,只能在 Ajax 请求完成并将 HTML 附加到 DOM 时呈现组件。

What would be the best approach to do that?最好的方法是什么?

The "best" approach is highly subjective. “最佳”方法是高度主观的。

However, you can certainly attach a component to an element if you know the element id:但是,如果您知道元素 id,您当然可以将组件附加到元素:

import component from 'Component.vue'
new Vue({
    el: '#targetId',
    render: h => h(component)
})

This assumes that you are using single file components.这假设您使用的是单个文件组件。 You can always create the component on the fly as well per the documentation :您也可以随时根据文档动态创建组件:

new Vue({
  el: '#targetId',
  // options
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM