mik*_*see 5 typescript polymer
我知道关于这个主题还有其他问题,但没有一个答案特别有用,有些已经过时,所以我想我会再问一次.
有没有人有幸得到Polymer和Typescript打得好看?看起来它几乎就在那里,理想情况下我们想要为聚合物提供一个类实例或原型,然后它会处理剩下的事情.
例如,给出以下内容:
<link rel="import" href="../bower_components/polymer/polymer.html">
<polymer-element name="my-element" attributes="color">
<template>
Hello there <strong>{{name}}</strong>
<button on-click="{{setFocus}}">Set focus to text input</button>
</template>
<script src="my-element.js"></script>
</polymer-element>
Run Code Online (Sandbox Code Playgroud)
如果我们这样做:
class MyElement {
name = "Mike";
setFocus() {
console.log("called");
}
}
Polymer(new MyElement());
Run Code Online (Sandbox Code Playgroud)
然后我们正确输出"名称"但单击按钮不会调用该功能.
但是,如果我们这样做:
class MyElement {
name = "Mike";
setFocus() {
console.log("called");
}
}
Polymer(MyElement.prototype);
Run Code Online (Sandbox Code Playgroud)
然后我们在单击按钮时获得控制台输出,但变量未定义.
任何人都有任何线索我们如何让这些发挥得很好?
默认字段值在构造函数中设置,但是当您将原型传递给Polymer()它时,它无权访问构造函数。
相反,在created回调中设置默认值。这有点像黑客,但它不仅限于 Typescript,我在编写用作聚合物元素的闭包编译器样式类时也遇到过同样的事情。
class MyElement {
name : string;
created() {
this.name = "Mike";
}
setFocus() {
console.log("called");
}
}
Polymer(MyElement.prototype);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2026 次 |
| 最近记录: |