我正在开发我的第一个 Dart 应用程序,已经完成了 Game of Darts 教程。我正在尝试创建一个语义命名的顶级菜单元素,该元素最终将在我的页面顶部显示导航菜单选项卡列表。我的 Dart 应用程序能够识别我的自定义元素并调用关联的构造函数。
但是,当我尝试在我的自定义元素中查询 UL 元素时,我得到了一个空引用。我需要 UL 参考才能将我的 LI 元素动态加载到菜单中。
问题 1:该元素是否应该在 DOM 中在构造函数运行时可见?
问题 2:如果还不可见,在自定义元素完全加载到 DOM 后,是否可以使用 Dart 事件来触发加载 LI 元素?
提前致谢!作为参考,这是我的自定义元素的来源:
topmenu-element.html
<!DOCTYPE html>
<html>
<body>
<element name="top-menu" constructor="TopMenu" extends="div">
<template>
<div>
Top Menu
<ul id="top-menu-list"></ul>
</div>
</template>
<script type="application/dart" src="topmenu-element.dart"></script>
</element>
</body>
</html>
topmenu-element.dart
import 'package:web_ui/web_ui.dart';
import 'dart:html';
class TopMenu extends WebComponent {
List menuItems = ['Session', 'Authentication Services', 'Vehicle Services', 'Subscriber Services', 'Data Services'];
void populateMenu() {
UListElement menuList = query('#top-menu-list');
LIElement newMenuItem = new LIElement();
newMenuItem.text = menuItems[0];
menuList.children.add(newMenuItem);
}
TopMenu() {
// populateMenu();
}
}