如何获取下面paper-input
元素的实例?
HTML文件:
<!DOCTYPE html>
<html>
<head>
<!-- <script src='packages/web_components/platform.js'></script>
not necessary anymore with Polymer >= 0.14.0 -->
<script src='packages/web_components/dart_support.js'></script>
<link rel='import' href='packages/paper_elements/paper_input.html'>
<script src='packages/browser/dart.js'></script>
</head>
<body fullbleed unresolved>
<paper-input id='subject' label='subject'></paper-input>
<script type='application/dart' src='myscript.dart'></script>
</body>
</html>
myscript.dart:
import 'dart:html';
import 'package:polymer/polymer.dart';
import 'package:paper_elements/paper_input.dart';
export 'package:polymer/init.dart';
PaperInput _subject = querySelector('#subject'); // exception
void main() {
...
}
这会导致异常:
Breaking on exception: type 'HtmlElement' is not a subtype of type 'PaperInput'
从HtmlElement转换为PaperInput不起作用。建议包括使用shadowRoot.querySelector('#subject');
和其他shadowRoot
用法,但它来自何处?如果我将.shadowRoot.querySelector('paper-input')
追加到myscript.dart的最后一行,则该元素为null。如果我使用id而不是标记名称,结果相同。元素本身是html主体的顶级,因此它不在任何其他shadowRoot中。
如果不是PaperInput类,则无法获取paper-input
的内容。但似乎没有办法施展它。
答案 0 :(得分:4)
你遇到how to implement a main function in polymer apps
当您执行此操作时,如下所示或在Polymer元素内部attached()
之后super.attached()
或某些单击或其他事件处理程序(当元素正确初始化时)
main() {
initPolymer().run(() {
// code here works most of the time
Polymer.onReady.then((_) {
// some things must wait until onReady callback is called
// for an example look at the discussion linked below
PaperInput _subject = querySelector('#subject');
});
});
}