AngularDI不会向上遍历实际的DOM层次结构
创始人
2024-10-24 12:01:37
0

在Angular应用中,DI依赖注入是一个非常强大且广泛使用的功能。然而,使用ng-content,Angular的继承关系不一定符合我们期望的DOM结构。当这种情况发生时,Angular DI可能会遇到问题,因为它不会向上遍历实际的DOM层次结构。

要解决这个问题,我们可以使用ViewChild,它允许我们在组件中获取对子组件元素的引用。下面是一个使用ViewChild解决DI向上遍历时遇到的问题的示例:

在子组件中声明一个元素,然后在父组件中使用ViewChild获取子组件的引用,并通过子组件的实例来调用子组件中的方法:

子组件中的代码:

import { Component } from '@angular/core';

@Component({
  selector: 'app-child',
  template: `
    
` }) export class ChildComponent { logMessage() { console.log('Button clicked!'); } }

父组件中的代码:

This is the content of the child component.

import { Component, ViewChild } from '@angular/core';
import { ChildComponent } from './child.component';

@Component({
  selector: 'app-parent',
  template: `
    

This is the content of the child component.

` }) export class ParentComponent { @ViewChild('childComponent') childComponent:

相关内容

热门资讯

截至目前"德普之星辅... 截至目前"德普之星辅助器app"pokemmo脚本手机版(一贯是真的辅助辅助器)-哔哩哔哩一、德普之...
于此同时"wepok... 于此同时"wepoker辅助器官方"wepoker作必弊视频(总是有辅助辅助器)-哔哩哔哩该软件可以...
透视美元局"poke... 透视美元局"pokemmo手机脚本"pokemmo脚本手机版(一直是有辅助工具)-哔哩哔哩1、该软件...
透视好友"wpk透视... 透视好友"wpk透视辅助下载"wepoker挂(总是存在有辅助下载)-哔哩哔哩wpk透视辅助下载脚本...
经调查"poker辅... 经调查"poker辅助器免费安装"fishpoker透视(原来存在有辅助工具)-哔哩哔哩1、首先打开...
备受关注的"wepo... 备受关注的"wepoker挂"红龙poker辅助(本来是真的辅助安装)-哔哩哔哩1、红龙poker辅...
透视讲解"wepok... 透视讲解"wepoker破解工具"wepoker免费脚本(原来有辅助插件)-哔哩哔哩1、超多福利:超...
目前来看"hhpok... 目前来看"hhpoker脚本"哈糖大菠萝有挂吗(都是真的是有辅助插件)-哔哩哔哩1)哈糖大菠萝有挂吗...
据玩家消息"德州透视... 据玩家消息"德州透视脚本"hhpoker智能辅助插件(切实存在有辅助软件)-哔哩哔哩1、玩家可以在h...
来临"wejoker... 来临"wejoker私人辅助软件"德普之星辅助器app(本来真的有辅助平台)-哔哩哔哩1、该软件可以...