1

我在 Angular 9 中创建了一个组件,并在组件中使用来注入 html。到目前为止,它仍然很正常(正确渲染html)。但是在构造函数中添加参数后,组件中的 html 不会在浏览器中呈现,只会在浏览器中呈现 ng-container 内容。我能怎么做?

我试图删除构造函数参数,然后组件正确渲染..但我需要在组件中导入render2和elementRef,所以我不能这样做

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.900.0-next.5
@angular-devkit/build-angular      0.900.0-next.5
@angular-devkit/build-ng-packagr   0.900.0-next.5
@angular-devkit/build-optimizer    0.900.0-next.5
@angular-devkit/build-webpack      0.900.0-next.5
@angular-devkit/core               9.0.0-next.5
@angular-devkit/schematics         9.0.0-next.5
@angular/cli                       9.0.0-next.5
@ngtools/webpack                   9.0.0-next.5
@schematics/angular                9.0.0-next.5
@schematics/update                 0.900.0-next.5
ng-packagr                         5.5.1
rxjs                               6.4.0
typescript                         3.5.3
webpack                            4.40.2

myComponent.component.ts

@Component({
  selector: '[nav-item]',
  templateUrl: './myComponent.component.html',
  styleUrls: ['./myComponent.component.scss'],
  encapsulation: ViewEncapsulation.None
})
....
constructor(private _renderer: Renderer2) {
    console.log('hee',_renderer);
  }

myComponent.component.html

<div class="nav_list_item--level1" >
    <a href="#" #navItemContent class="nav_list_item_content nav_list_item_content--level1"  >
        <ng-content select="[nav-content]"></ng-content>
    </a>
    <div #navItemDetail class="nav_list_item_detail nav_list_item_detail--level1">
        <ng-content select="[nav-detail]"></ng-content>
    </div> 
</div>

app.component.html

<div nav-item>
   <ng-container nav-content>
      content text
   </ng-container>
   <ng-container nav-detail>
      <div style="width: 800px;">
        detail detail text
      </div>           
   </ng-container>
</div>

但是当在 chrome 中呈现时,组件中的 html 不会在浏览器中呈现....

<div nav-item>
content text
<!--ng-container-->
detail detail text
<!--ng-container-->
</div>

正确的渲染如下

<div nav-item>
<a href="#" #navItemContent class="nav_list_item_content nav_list_item_content--level1"  >
        content text
    </a>
    <div #navItemDetail class="nav_list_item_detail nav_list_item_detail--level1">
        detail detail text
    </div> 
</div>
4

0 回答 0