代码之家  ›  专栏  ›  技术社区  ›  Jerald

在角6中获取不带@view child decorator的view子元素

  •  0
  • Jerald  · 技术社区  · 7 年前

    我有一个自定义组件的装饰器:

    export const MyDecorator = () => {
      return (constructor: Function) => {
        constructor.prototype['ngAfterViewInit'] = function() {
          // I need to get component's view child here
        };      
      };
    };
    

    我需要在decorator中获得一个view子元素。在组件中,我可以使用@viewchild decorator获取此元素:

    @MyDecorator()
    @Component({
      selector: "app-my-component",
      templateUrl: "./my-component.html"
    })
    export class MyPage {
      @ViewChild(Content) content: Content;
    }
    

    如何在mydecorator中获取content元素?

    1 回复  |  直到 7 年前
        1
  •  1
  •   magos    7 年前

    无法在组件初始化之前引用该组件的dom。如果使用类装饰器,则不能对不存在的文档结构进行操作。我建议将这个逻辑移到组件中。

    属性decorator允许您将dom元素作为参数传递。

    推荐文章