gpt4 book ai didi

html - 在另一个自定义元素的模板中实例化时,自定义元素未检测到子元素

转载 作者:行者123 更新时间:2023-12-03 03:46:29 25 4
gpt4 key购买 nike

我有一个RPG Main HTML元素,定义如下。我正在index.html文件的正文中实例化它。

<link rel="import" href="packages/polymer/polymer.html">
<link rel="import" href="gridlayout.html">
<link rel="import" href="gridtile.html">


<polymer-element name="rpg-main" attributes="">
<template>
<style>
grid-tile {
background: #FF00FF;
width: 50px;
height: 50px
}
:host {
position: absolute;
display: block;
width: 500px;
height: 500px;
}

</style>
<grid-layout rows = "2" cols = "2" spacing = "50px">
<grid-tile>
</grid-tile>
<grid-tile>
</grid-tile>
<grid-tile>
</grid-tile>
<grid-tile>
</grid-tile>
</grid-layout>
</template>
<script type="application/dart" src="rpgmain.dart"></script>
</polymer-element>

不出所料,我还定义了grid-layout和grid-tile元素。在网格布局的构造函数中,我尝试引用其子级。但是元素在上面的rpg-main类中实例化时认为它有0个 child 。
import 'package:polymer/polymer.dart';
import 'dart:html';

@CustomTag('grid-layout')
class GridLayout extends PolymerElement {
@published int rows;
@published int cols;
@published int spacing;

String _px = "px";
String _perc = "%";

GridLayout.created() : super.created() {

print("NUM CHILDREN " + this.children.length.toString());

/** for (int i = 0; i <= this.rows + this.cols - 2; i++) {
Element child = this.children[i];
this.children[i].style.margin = this._parseNum(spacing);
child.style.float = "right";

if (i % this.rows == this.rows) {
child.style.clear = "right";
}


}**/
}
num _parseString(String s) {
print(s.toString());
return num.parse(s.split(_px)[0]);
}

String _parseNum(num n) {
return n.toString() + _px;
}
}

***上面的代码显示“NUM CHILDREN:0”

它只是在我的rpg-main元素中实例化,所以令我惊讶的是它没有将grid-tiles识别为 child 。可能是因为在rpg-main自定义元素的模板标签中实例化了grid-layout元素吗? (因此,也许网格布局不认为其子级位于“轻型dom”中?)这将是一个耻辱,但如果是这样,解决方法将是什么?

最佳答案

构造函数只是错误的地方。您需要先允许元素正确初始化,然后再使用它们。

尝试

@override
void attached() {
super.attached();
print("NUM CHILDREN " + this.children.length.toString());
}

另请参阅Justins在这里回答 When is shadowRoot available to a polymer component?

关于html - 在另一个自定义元素的模板中实例化时,自定义元素未检测到子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24499713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com