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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Angular 内容投影出现 No provider for TemplateRef found 错误的单步调试

發布時間:2023/12/19 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Angular 内容投影出现 No provider for TemplateRef found 错误的单步调试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

問題描述

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

我有一個能接受內容投影的 Angular Component:

使用如下代碼消費這個 Component:

但是遇到運行時錯誤,如下圖所示。
點擊 template.html:7:3:


問題分析

拋出該錯誤消息的函數實現:

/*** Returns the value associated to the given token from the NodeInjectors => ModuleInjector.** Look for the injector providing the token by walking up the node injector tree and then* the module injector tree.** This function patches `token` with `__NG_ELEMENT_ID__` which contains the id for the bloom* filter. Negative values are reserved for special objects.* - `-1` is reserved for injecting `Injector` (implemented by `NodeInjector`)** @param tNode The Node where the search for the injector should start* @param lView The `LView` that contains the `tNode`* @param token The token to look for* @param flags Injection flags* @param notFoundValue The value to return when the injection flags is `InjectFlags.Optional`* @returns the value from the injector, `null` when not found, or `notFoundValue` if provided*/ function getOrCreateInjectable(tNode, lView, token, flags = InjectFlags.Default, notFoundValue)

第一個輸入參數 tnode 就是 p 節點本身:

token 指向 TemplateRef:

解決方案

這個問題有幾種解決方案。

方案1

刪除 p 節點的自定義 Directive:

然后添加一個默認的內容投影:

解決方案2

刪除自定義 Directive 構造函數里的 TemplateRef 依賴:

解決方案3

消費 zippy Component 時,直接傳入被投影的內容:

該內容會被投影到下圖第一行的 place holder:

解決方案4

消費 Component 時,通過 ng-template + 自定義 Directive 的方式指定被投影的內容:

通過 @ContentChild conten query 拿到自定義 Directive 實例,進而拿到 Directive 指向的 TemplateRef:

總結

渲染樹是實際的 DOM 渲染樹。 它與 Ivy 的邏輯樹不同,渲染樹必須考慮內容投影。 因此渲染樹里的父/子關系不像邏輯視圖中那樣簡單。

總結

以上是生活随笔為你收集整理的Angular 内容投影出现 No provider for TemplateRef found 错误的单步调试的全部內容,希望文章能夠幫你解決所遇到的問題。

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