日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Angular 内容投影 content projection 关于条件渲染问题的单步调试

發布時間:2023/12/19 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Angular 内容投影 content projection 关于条件渲染问题的单步调试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

問題描述

本文涉及到的代碼位置:https://github.com/wangzixi-diablo/ngDynamic

ng-container 和 ngTemplateOutlet 的配合使用。

<ng-container [ngTemplateOutlet]="content.templateRef"></ng-container>

這里需要接受一個類型為 TemplateRef 的輸入:

content.templateRef 在哪里賦的值?

使用了 ContentChild 這個 content query:

在運行時,content 的值為施加了 ZippyContentDirective 的 ng-template 代表的 TemplateRef 實例。

這個 Directive 對應的選擇器為:appExampleZippyContent

因此也就是下圖這個模板:

我期望最終運行時,能夠在頁面看到上圖模板變量的值被內容投影到 app-example-zippy 內部。

然而實際結果是:

控制臺有錯誤報出:

ERROR TypeError: Cannot read properties of undefined (reading ‘templateRef’)
at ZippyComponent_div_1_Template (template.html:3:19)
at executeTemplate (core.js:7511:9)
at refreshView (core.js:7380:13)
at refreshEmbeddedViews (core.js:8481:17)
at refreshView (core.js:7404:9)
at refreshComponent (core.js:8527:13)
at refreshChildComponents (core.js:7186:9)
at refreshView (core.js:7430:13)
at refreshComponent (core.js:8527:13)
at refreshChildComponents (core.js:7186:9)

問題分析

單擊 template.html 進入具體引起錯誤的代碼位置:

自動導航到上圖第三行:content.templateRef, 運行時 content 的值為undefined,因為試圖訪問 undefined 的 templateRef 屬性,所以報錯。

content 的值為空,說明下圖 content query 執行失敗了:

@ContentChild(ZippyContentDirective)

我在這個自定義 Directive 的構造函數里設置了斷點,但是運行時斷點根本就未觸發,這只能說明,Angular 框架根本就沒有識別出該 Directive:

究其原因,在自定義 Directive 所在的 NgModule 定義的 declarations 區域里,沒有將該自定義 Directive 添加進去。

將上圖21行代碼取消注釋之后,自定義 Directive 的構造函數立即被調用了:

注意這里的調用上下文:當自定義 Directive 被添加到 NgModule 的 declarations 區域后,一旦模板解析邏輯檢測到該 Directive,就會調用 Angular 的依賴注入相關框架代碼,自動生成 Directive 實例:

if (isDirectiveHost(tNode)) {createDirectivesInstances(tView, lView, tNode);}

總結

本文采取的自定義 Directive,本質上是一個 anchor,用于定位將要被內容投影的模板實例 TemplateRef.

使用 TemplateRef,組件可以使用 ngTemplateOutlet 指令或 ViewContainerRef 方法 createEmbeddedView() 呈現引用的內容。

總結

以上是生活随笔為你收集整理的Angular 内容投影 content projection 关于条件渲染问题的单步调试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。