【问题标题】:Vue: reinitiating Vue after fetching new components and inserting them into HTMLVue:在获取新组件并将它们插入 HTML 后重新启动 Vue
【发布时间】:2018-07-10 08:11:43
【问题描述】:

在我从 API 获取组件并将其插入 DOM 之后,是否可以强制 Vue 重新启动自身?

我的意思是组件:

<div><My_component /></div>

这是 BE sn-p 的一部分。

并将其插入 HTML:

document.getElementById("something").innerHTML = "<div><My_component /></div>"

我已经尝试过 .$forceUpdate() 函数,但它只是返回 undefined 并且什么也没做。

我无法让新添加的组件被 Vue 识别。有什么办法吗?

【问题讨论】:

    标签: javascript dom vue.js rendering


    【解决方案1】:

    您可能希望根据需要使用动态组件。

    这里是快速语法。

    JSON

    var items = [
      {component: 'TextComponent', params: {x: 1, y: 2}},
      {component: 'BoxComponent', params: {a: 3, b: 2}}
    ]
    

    Vue 模板

    <component :is="item.component" v-for="item in items" v-bind="item.params" />
    

    将呈现给

    <TextComponent :x="1" :y="2" />
    <BoxComponent :a="3" :b="2" />
    

    【讨论】:

    • 问题是我的组件在一个后端 HTML sn-p 中,被其他 HTML 包围,这个 sn-p 一会儿被发送到 FE 并插入到 DOM 中。这仍然适用吗?
    • 我认为这不可能,但您可以将 html 转换为 json 可能会有所帮助。 jew.ski/himalaya - github.com/andrejewski/himalaya
    • @Jousi check $compile,我相信这就是你要找的。 vuejs.org/v2/api/#Vue-compile
    猜你喜欢
    • 1970-01-01
    • 2018-10-08
    • 2019-07-19
    • 2021-04-05
    • 2017-11-09
    • 2021-01-27
    • 1970-01-01
    • 2021-04-19
    • 2018-11-10
    相关资源
    最近更新 更多