【问题标题】:Angular Transclusion: X is not a known element角度嵌入:X 不是已知元素
【发布时间】:2017-09-26 14:51:58
【问题描述】:

我正在关注few 好的posts 关于角度嵌入的内容来制作向导组件。我有一个 Wizard 组件,它采用 WizardStep 组件。我遇到的问题是我的向导模板 (wizard-title) 中的待嵌入元素是:

Unhandled Promise rejection: Template parse errors:
'wizard-title' is not a known element:
1. If 'wizard-title' is an Angular component, then verify that it is part of this module.
2. If 'wizard-title' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.

我知道上面的错误很清楚,但这是一个 Web 组件吗?

一些代码:

wizard.component.html

(包装元素,选择器:reg-wizard):
<md-card>
  <md-card-header>
    <ng-content select="wizard-title"></ng-content> 
  </md-card-header>
  <md-card-content>
    <ng-content select="reg-wizard-step"></ng-content> <!-- STEPS, corresponds to another component -->
  </md-card-content>
</md-card>

wizard-step.component.html

(重复元素,选择器:reg-wizard-step):
<md-card>
  <md-card-header>
    <ng-content select="wizard-step-header"></ng-content>
  </md-card-header>
  <md-card-content>
    <ng-content select="wizard-step-content"></ng-content>
  </md-card-content>
</md-card>

测试实现(在app.component.html):

<reg-wizard>
  <wizard-title>Title</wizard-title>

  <reg-wizard-step>
    <wizard-step-header>Step 1</wizard-step-header>
    <wizard-step-content>
      <p>some paragraph in step 1</p>
      <button md-raised-button>next</button>
    </wizard-step-content>
  </reg-wizard-step>

  <reg-wizard-step>
    <wizard-step-header>Step 2</wizard-step-header>
    <wizard-step-content>
      <p>some paragraph in step 2</p>
      <button md-raised-button>finish</button>
    </wizard-step-content>
  </reg-wizard-step>
</reg-wizard>

预期输出:

<md-card>
  <md-card-header>
    Title
  </md-card-header>
  <md-card-content>

    <md-card>
      <md-card-header>
        Step 1
      </md-card-header>
      <md-card-content>
        <p>some paragraph in step 1</p>
        <button md-raised-button>next</button>
      </md-card-content>
    </md-card>

    <!--some functions / css will hide step 2 until it is needed...-->
    <md-card>
      <md-card-header>
        Step 2
      </md-card-header>
      <md-card-content>
        <p>some paragraph in step 1</p>
        <button md-raised-button>next</button>
      </md-card-content>
    </md-card>

  </md-card-content>
</md-card>
我的版本:
@angular/cli: 1.0.0
node: 7.7.3
os: darwin x64
@angular/animations: 4.1.0
@angular/common: 4.1.0
@angular/compiler: 4.1.0
@angular/core: 4.1.0
@angular/forms: 4.1.0
@angular/http: 4.1.0
@angular/material: 2.0.0-beta.3
@angular/platform-browser: 4.1.0
@angular/platform-browser-dynamic: 4.1.0
@angular/router: 4.1.0
@angular/cli: 1.0.0
@angular/compiler-cli: 4.1.0

【问题讨论】:

  • 您是否将向导模块添加到您的应用程序模块中?
  • 它可能在某个时候应该有自己的模块,但我在app.module中声明了这两个组件
  • 您应该将它自己的模块添加到您应用程序模块中的includes
  • 尝试发布向导模块和组件的代码。与它相关的模块和组件(您的应用程序、您的视图等)也是如此。所以我们可以帮助你! :D

标签: angular transclusion


【解决方案1】:

再一次,Angular 的错误消息在这里非常清晰和有用。 This answer 为我指明了正确的方向。

如果“wizard-title”是一个 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“@NgModule.schemas”以禁止显示此消息。

我不知道这一点,但那些被嵌入的元素似乎是 Web 组件。添加

schemas: [ CUSTOM_ELEMENTS_SCHEMA ]

我的应用模块似乎已经解决了这个问题;但是,我不知道为什么博文中的示例不必有这个。

看起来您也可以为这些suggested here 创建自己的指令:

@Directive({selector: 'my-component-title, my-component-content'})
class MyComponentTags{ }

export const MY_COMPONENT_DIRECTIVES = [ MyComponent, MyComponentTags ];

【讨论】:

  • 就我而言,它与 Web 组件无关。但是schemas: [ CUSTOM_ELEMENTS_SCHEMA ] 也有帮助!也许这个模式比 Web Components 更通用?
猜你喜欢
  • 2018-04-10
  • 1970-01-01
  • 2021-01-22
  • 2021-01-13
  • 1970-01-01
  • 1970-01-01
  • 2020-10-24
  • 2021-04-04
  • 2017-07-27
相关资源
最近更新 更多