ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-03-05 11:02:28  阅读:209  来源: 互联网

标签:core projection Directive js content 单步 TemplateRef 自定义


问题描述

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

ng-containerngTemplateOutlet 的配合使用。

<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 被添加到 NgModuledeclarations 区域后,一旦模板解析逻辑检测到该 Directive,就会调用 Angular 的依赖注入相关框架代码,自动生成 Directive 实例:

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

总结

本文采取的自定义 Directive,本质上是一个 anchor,用于定位将要被内容投影的模板实例 TemplateRef.

使用 TemplateRef,组件可以使用 ngTemplateOutlet 指令或 ViewContainerRef 方法 createEmbeddedView() 呈现引用的内容。

标签:core,projection,Directive,js,content,单步,TemplateRef,自定义
来源: https://www.cnblogs.com/sap-jerry/p/15967233.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有