【问题标题】:angular2 "not a known element" bug : everything was triedangular2“不是已知元素”错误:一切都已尝试
【发布时间】:2018-02-24 10:59:42
【问题描述】:

我尝试在另一个模块中使用某些组件的非常经典的情况:

外来组件:

import { Component, ViewEncapsulation,
  ElementRef, ViewChild, Input, Output,
  EventEmitter } from '@angular/core';

declare var __moduleName: string

@Component({
  moduleId:    __moduleName,
  selector:    'bidule',
  templateUrl: 'bidule.html',
  styleUrls:   [],
  encapsulation: ViewEncapsulation.None
})
export class BiduleComponent {

  @Input() config: { }

  constructor(protected elRef: ElementRef) {
  }

}
// template : <p>basic text</p>

国外模块:

import { NgModule }       from '@angular/core';
import { CommonModule }   from '@angular/common';

import { BiduleComponent } from './bidule'

@NgModule({
  imports: [
  ],
  declarations: [
    BiduleComponent
  ],
  exports: [
    BiduleComponent
  ]
})
export class BiduleModule {}

主要组件:

...
import { BiduleComponent }       from '../../bidule/bidule'
...
// template : <bidule></bidule>

主模块:

import { BiduleModule }          from '../../bidule/module'
// ...
@NgModule({
  imports: [
    // ...
    BiduleModule
  ]
})

我相信我检查了一切:

https://stackoverflow.com/questions/44429996#44430230

如果你有任何想法。提前致谢。

(bidule 是 thingamajig 的法语单词)

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    您只需要在主模块中导入Foreign模块,并在主模块的任何组件上使用&lt;bidule&gt;&lt;/bidule&gt;即可。 导出 BiduleComponent 是对的,但不需要直接导入 Main Component,将 Foreign Module 导入 Main Module 就足够了。

    【讨论】:

    • 它仍然存在,否则我会验证你的答案:)
    • 尝试删除moduleId和封装,并请从主组件的模板中删除//。还要使您的代码尽可能简单地开始(如果不需要,请从构造函数中删除声明)。能够确定问题开始出现的位置。
    猜你喜欢
    • 2017-05-07
    • 1970-01-01
    • 2017-01-13
    • 2019-06-18
    • 2017-08-20
    • 2017-03-10
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多