首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >获取组件选择器

获取组件选择器
EN

Stack Overflow用户
提问于 2017-04-05 10:22:33
回答 2查看 1.1K关注 0票数 0

‘’epimss personnel‘给出了以下内容...

@Component(
    {
      selector: 'epimss-personnel',
      templateUrl: './personnel.component.html',
      styleUrls: [ './personnel.component.css' ]
    } )
export class PersonnelComponent implements OnInit {
  personnel: IPersonnel = new Personnel();

  constructor( ) {
  }

  ngOnInit() {
  }
}

..。如何在PersonnelComponent中以编程方式获取选择器字符串'epimss-personnel‘?

编辑1

当我添加了下面的建议时,我得到了以下信息:

compiler.es5.js:14582 Uncaught Error: Unexpected value 'AppRoutingModule' imported by the module 'AppModule'. Please add a @NgModule annotation.
    at syntaxError (compiler.es5.js:1503) [<root>]
    at :4200/vendor.bundle.js:117324:44 [<root>]
    at Array.forEach (<anonymous>) [<root>]
    at CompileMetadataResolver.getNgModuleMetadata (compiler.es5.js:13953) [<root>]
    at JitCompiler._loadModules (compiler.es5.js:25134) [<root>]
    at JitCompiler._compileModuleAndComponents (compiler.es5.js:25093) [<root>]
    at JitCompiler.compileModuleAsync (compiler.es5.js:25055) [<root>]
    at PlatformRef_._bootstrapModuleWithZone (core.es5.js:4786) [<root>]
    at PlatformRef_.bootstrapModule (core.es5.js:4772) [<root>]
    at Object.426 (main.ts:47) [<root>]
    at __webpack_require__ (bootstrap d227106…:52) [<root>]
    at Object.1132 (main.bundle.js:7) [<root>]
    at __webpack_require__ (bootstrap d227106…:52) [<root>]
    at webpackJsonpCallback (bootstrap d227106…:23) [<root>]

..。UI也不会显示。

EN

回答 2

Stack Overflow用户

发布于 2019-05-16 07:04:37

如果您不在组件内部,您将无法使用ElementRef,并且Reflect解决方案将不能与AOT一起工作。幸运的是,当您知道目标是一个组件时,还有一种替代方法:

const components = [ PersonnelComponent ];
/*
  ...
  Anything that can be injected into
*/
constructor(private componentFactoryResolver: ComponentFactoryResolver) {
  // For each component
  components.forEach(element => {
    // Get its resolved factory
    const factory = this.componentFactoryResolver.resolveComponentFactory(element);

    console.log('Factory:', factory);
    console.log('Selector:', factory.selector);
  });
}
票数 2
EN

Stack Overflow用户

发布于 2017-04-12 23:44:00

constructor(elRef:ElementRef) {
  console.log(elRef.nativeElement.tagName);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43220746

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档