双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

Angular QueryList 在什么情况下查询不到数据

chanchaw chanchaw 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
Angular7中自己制作了directive,名称是:NextTabDirective
在一个业务组件中通过QueryList查询获取该类型的DOM元素
@ViewChildren(NextTabDirective) inputs: QueryList<NextTabDirective>;

然后在组件的 ngAfterViewInit 中打印出来
结果浏览器控制台中打印出来显示没有获取到数据


都有哪些问题会导致使用QueryList查询不到结果?

自己已经确认的问题:
1. 自己制作的directive 名称是 NextTabDirective ,这点肯定没有搞错,名称是复制过来的
2. 该directive 在项目的根模块 app.module.ts 中通过import 引入:
import { NextTabDirective } from './shared/ccDirective/next-tab.directive';


同时在 @NgModule 的 declarations 中声明了。
3. 业务组件的ts文件中也在头部(最上面)引入了:
import { NextTabDirective } from 'src/app/shared/ccDirective/next-tab.directive';


4. 项目结构是:
app
|--->app.module.ts
|--->shared\ccDirective\next-tab.directive.ts
|--->pages\order\order.component.html <-- 业务组件html模板文件
|--->pages\order\order.component.ts <-- 业务组件ts文件

5. 下面是自定义directive的全部代码

import { Directive, HostListener, ElementRef } from '@angular/core';

@Directive({
selector: '[next-tab]'
})
export class NextTabDirective {

self:any;
nextControl:any;

@HostListener("keydown.enter", ["$event"])
onEnter(event: KeyboardEvent) {
if (this.nextControl) {
if (this.nextControl.focus) {
this.nextControl.focus();
this.nextControl.select();
event.preventDefault();
return false;
}
}
}

constructor(private control: ElementRef) {
this.self=control.nativeElement;
}
}


6. order.component.html 的标签中使用自定义directive的代码如下:
<input type="text" next-tab>




疑问:
同样的方法,单独制作一个项目测试通过的,把这些代码搬到我开发中的项目里就获取不到数据
-- 通过
@ViewChildren(NextTabDirective) inputs: QueryList<NextTabDirective>;

获取不到

我哪里错了?thanks for any help..
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
chanchaw
JavaScript 87,983 成员 224,680 内容