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

chanchaw 2019-01-11 10:20:50
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..
...全文
607 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
chanchaw 2019-01-13
  • 打赏
  • 举报
回复
明天还没有合适回答再结贴吧
chanchaw 2019-01-12
  • 打赏
  • 举报
回复
引用 3 楼 讨厌走开啦 的回复:
[quote=引用 2 楼 chanchaw 的回复:] [quote=引用 1 楼 讨厌走开啦 的回复:] 把QueryList的代码贴出来看看。
QueryList 是Angular内嵌的函数。。[/quote] 我是说你怎么调用的,你是不是查询了directive作为dom的数量,但是实际上directive是作为attribute在使用。[/quote] ***************************************************************************** 是你说的逻辑 在html模板文件中的调用: <input type="text" next-tab> 这里的next-tab就是我自定义的directive 然后ts文件中获取所有使用了next-tab属性的标签,代码如: @ViewChildren(NextTabDirective) inputs: QueryList<NextTabDirective>; 然后我在ngAfterViewInit()函数里调用console.log(this.inputs) 来打印 打印显示是空数组
讨厌走开啦 2019-01-12
  • 打赏
  • 举报
回复
引用 2 楼 chanchaw 的回复:
[quote=引用 1 楼 讨厌走开啦 的回复:] 把QueryList的代码贴出来看看。
QueryList 是Angular内嵌的函数。。[/quote] 我是说你怎么调用的,你是不是查询了directive作为dom的数量,但是实际上directive是作为attribute在使用。
chanchaw 2019-01-12
  • 打赏
  • 举报
回复
引用 1 楼 讨厌走开啦 的回复:
把QueryList的代码贴出来看看。
QueryList 是Angular内嵌的函数。。
chanchaw 2019-01-12
  • 打赏
  • 举报
回复
引用 5 楼 讨厌走开啦 的回复:
@ViewChildren(NextTabDirective) inputs: QueryList<NextTabDirective>; 这一句 你怎么区分是获取directive作为dom使用的数量还是获取作为attribute使用的数量?
********************************************************** 我的html文件中是通过: <input type="text" next-tab> 的方式来使用的,那么就是获取作为attribute使用的数量了 这样做有问题? 我在一个空白的工程中测试是成功的
讨厌走开啦 2019-01-12
  • 打赏
  • 举报
回复
@ViewChildren(NextTabDirective) inputs: QueryList<NextTabDirective>; 这一句 你怎么区分是获取directive作为dom使用的数量还是获取作为attribute使用的数量?
讨厌走开啦 2019-01-11
  • 打赏
  • 举报
回复
把QueryList的代码贴出来看看。

87,996

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧