【问题标题】:'foo' is not a known element when dynamically rendering HTML动态呈现 HTML 时,'foo' 不是已知元素
【发布时间】:2018-07-03 23:14:47
【问题描述】:

我正在尝试在 Angular 中动态呈现包含 HTML 标记的字符串。 HTML 应该是“Angular 编译的”,即包括数据绑定和组件渲染(我在 AngularJS 中使用 $compile 所做的事情)。

我大部分时间都在使用p3x-angular-compile:

<div [p3x-compile]="Template.Source" [p3x-compile-ctx]="Data"></div>

按预期工作并正确呈现Template.Source,即:

this.Template.Source = '<p>Hello</p>';

还有

this.Template.Source = '<p>{{Foo}}</p>';

其中 Foo 是绑定 Data 对象上的一个属性。

但是,渲染我自定义的角度组件不起作用:

this.Template.Source = '<app-sc-navbar></app-sc-navbar><p>Other arbitrary markup anywhere in string'</p>;

产生错误:

编译属性.js:80

错误:模板解析错误:“app-sc-navbar”不是已知元素: 1. 如果“app-sc-navbar”是一个 Angular 组件,那么验证它是这个模块的一部分。 2. 如果 'app-sc-navbar' 是一个 Web 组件,则将 'CUSTOM_ELEMENTS_SCHEMA' 添加到该组件的 '@NgModule.schemas' 禁止显示此消息。

组件 (ScNavbarComponent) 是应用模块声明的一部分,并且 - 如果在静态标记中使用 - 可以正常工作。

我在这里缺少什么?如何让动态渲染知道ScNavbarComponent?

编辑:

这是完整的调试器输出:

我已尝试将ScNavbarComponent 添加到导出和/或入口组件中。

@NgModule({
  declarations: [
    AppComponent,
    ScNavbarComponent,
    ...
  ],
  imports: [
    BrowserModule,
    ...
    CompileModule
  ],
  entryComponents: [
    ScNavbarComponent
  ],
  providers: [],
  bootstrap: [AppComponent],
  exports: [
    ScNavbarComponent
  ],
})
export class AppModule { }

【问题讨论】:

  • 我猜你应该将ScNavbarComponent 添加到AppModule 的entryComponents 数组中
  • @PierreDuc 谢谢!我已经尝试过了,但它不会改变行为。我已在我的问题中附加了更多信息。
  • 你用的是什么角度版本?
  • @PierreDuc Angular 6.0.3

标签: angular p3x-angular-compile


【解决方案1】:

如果您想在一个模块中声明组件并在另一个模块中使用它们,您需要导出它们,以便您能够在另一个模块中导入该模块。

在您的 app.module.ts 中声明并导出它们,以便您的其他模块可以理解这些来自另一个模块。

 @NgModule({
  imports: [
    BrowserModule
  ],

  declarations: [
    AppComponent,
    ScNavbarComponent
  ],

exports: [
      ScNavbarComponent
],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {

 }

在您的其他模块中,您现在可以导入 ScNavbarComponent。

如果您没有不同的模块,您只需将 ScNavbarComponent 添加到 app.module.ts 的 entryComponents 部分。如果它还没有,你可以像下面这样添加它。

entryComponents: [
    ScNavbarComponent
  ]

编辑:

您可能会考虑做的事情(这可能更适合您的目标)是使用 ComponenFactoryResolver,它允许动态渲染 Angular 组件。下面是一个如何工作的例子:

在您的模板中,您可以使用如下模板引用:

<div #navbar></div>

要将组件分配给此 ref,在您的组件中,您应该使用 ViewChild 注释来引用它:

@ViewChild('navbar', { ViewContainerRef }) navBar: ViewContainerRef;

接下来,您应该在构造函数中注入解析器本身:

constructor(private resolver: ComponentFactoryResolver) {}

解析器现在可以使用了,应该在生命周期钩子 ngAfterContentInit 中使用,如下所述(确保您的组件实现 ngAfterContentInit):

ngAfterContentInit() {
  const navBarFactory = this.resolver.resolveComponentFactory(ScNavbarComponent); // make sure to import your component
  const component = this.navBar.createComponent(navBarFactory);
}

实现上述代码后,您的 ScNavbarComponent 应该会动态投影到您的 templateRef 中。

我希望这会有所帮助!

【讨论】:

  • 非常感谢!我已经尝试了这两种方法,但它们并没有改变行为。我已在问题中附加了更多信息。目前,我只有一个模块,它声明了 App 和 NavbarComponent。有更多想法吗? ;-)
  • 完全没问题!您能否仔细检查 ScNavbarComponent 的选择器的名称。你确定是“app-sc-navbar”吗?
  • 我用另一个您可能考虑尝试的选项更新了我的答案。让我知道这是否适合您!
  • 非常感谢!我之前读过ComponentFactoryResolver,但不幸的是它不符合我的要求。我正在开发一种 CMS,高级用户应该能够在编辑器中编辑呈现的标记,其中标记可以包含几个预定义的指令。据我了解ComponentFactoryResolver 和您的示例,它只适合在运行时创建特定组件的实例。我还没有找到呈现完整标记字符串的链接。该示例在我的问题中具有误导性(标记中只有一个标签)。我会编辑它。谢谢。
  • 啊好吧,我明白了。我偶然发现了这篇似乎符合您需求的文章:gearheart.io/blog/…。但我不确定这是否也适用于完整的标记字符串。这种技术也被称为 $compile 等价物。
猜你喜欢
  • 2023-03-09
  • 2019-04-25
  • 2017-07-27
  • 1970-01-01
  • 1970-01-01
  • 2021-12-09
  • 1970-01-01
  • 2017-05-07
  • 2011-08-16
相关资源
最近更新 更多