我的程序运行正常,直到有必要构建一个使用自定义Web组件的库。我无法理解Dart在抱怨什么。它给出了一个警告,说它“无法解析my_library”导致错误,“没有这种类型的WebComponent”。我的尝试基于this。这是我的代码:
myapp.dart:
library my_library;
import 'dart:html';
import 'package:web_ui/web_ui.dart';
part 'fancyoption.dart';
void main() {
// Enable this to use Shadow DOM in the browser.
//useShadowDom = true;
}
myapp.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sample app</title>
<link rel="stylesheet" href="myapp.css">
<link rel="components" href="fancyoption.html">
</head>
<body>
<h3>Type a color name into a fancy option textbox, push the button and
see what happens!</h3>
<div is="x-fancy-option" id="fancy-option1"></div>
<div is="x-fancy-option" id="fancy-option2"></div>
<div is="x-fancy-option" id="fancy-option3"></div>
<script type="application/dart" src="myapp.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>
fancyoption.dart(在同一个web / out目录中):
part of my_library;
class FancyOptionComponent extends WebComponent {
ButtonElement _button;
TextInputElement _textInput;
FancyOptionComponent() {
}
void inserted() {
_button = this._root.query('.fancy-option-button');
_textInput = this._root.query('.fancy-option-text');
// make the background color of this web component the specified color
final changeColorFunc = (e) => this.style.backgroundColor = _textInput.value;
_button.onClick.listen(changeColorFunc);
}
}
fancyoption.html:
<!DOCTYPE html>
<html>
<body>
<element name="x-fancy-option" constructor="FancyOptionComponent" extends="div">
<template>
<div>
<button class='fancy-option-button'>Click me!</button>
<input class='fancy-option-text' type='text'>
</div>
</template>
<script type="application/dart" src="fancyoption.dart"></script>
</element>
</body>
</html>
答案 0 :(得分:0)
正确的答案发表在评论中 - 将克里斯·巴克特的答案重新作为答案,为未来的读者带来好处。
“Web组件是一个独立的,可重用的组件,因此fancyoption.dart不应该是主应用程序的一部分。”