【问题标题】:Angular equivalent of Vue().$mount()Vue().$mount() 的 Angular 等价物
【发布时间】:2021-09-11 12:04:25
【问题描述】:

我正在创建一个基于 JSON 表单构建 HTML DOM 的应用程序。此表单是可扩展的,并允许添加自定义控件。一件重要的事情是让它与不同的 JavaScript 框架一起工作。

我正在尝试使用 Angular 生成一个虚拟 DOM,但我不想将它附加到 document,而是希望将 DOM 元素包含在一个变量中。

这是我用 Vue 设法做到的方式:

const app = new Vue({
    render(createElement) {
        return createElement(MyCustomControl, {}, this.$slots.default);
    },
    components: { MyCustomControl }
}).$mount();

const customControl = document.createElement("div").appendChild(app);

有没有办法在 Angular 中做类似的事情?

【问题讨论】:

  • 据我所知,Angular 无法做到这一点,难道你不能使用动态组件吗? angular.io/guide/dynamic-component-loader。想法是您将根据获得的数据动态创建组件。类似于 Vue 中的动态组件。
  • 相当于bootstrap()。您需要为其提供一个元素。我不记得它是否适用于分离的节点。

标签: javascript angular vue.js dom


【解决方案1】:

app.module.ts

@NgModule({
  ...
  bootstrap: [AppComponent] // You can modify it here
})

【讨论】:

    猜你喜欢
    • 2019-12-23
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    • 2018-09-29
    • 2016-04-19
    • 2013-09-16
    • 1970-01-01
    相关资源
    最近更新 更多