gpt4 book ai didi

dart - 数据绑定(bind)结构化对象在我的 polymer 元素上没有显示任何变化

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

我正在将应用程序从 0.5 迁移到 Polymer 1 并使用 Dart。我做得很好,但现在我面临一些数据绑定(bind)问题:

我创建了一个 polymer 元素,它以编程方式在其本地 DOM 上插入另一个 polymer 元素,提供联系人信息。第二个元素将从构造函数中获取信息,并通过数据绑定(bind)将其显示在界面上。

家长 Dart :

@PolymerRegister('test-app')
class TestApp extends PolymerElement {
TestApp.created() : super.created();

attached() {
super.attached();
async(() {
insertTile();
});
}

void insertTile() {
String contactJSON = '{"contactId":"1", "firstName":"Lex", "surname":"Luthor"}';
Contact contact = new Contact(JSON.decode(contactJSON));
ContactTile tile = new ContactTile(contact, this);
Polymer.dom(this.root).append(tile);
}
}

父 html:
<dom-module id="test-app">
<style>
:host {
display: block;
}
</style>

<template>
</template>
</dom-module>

child Dart :
@PolymerRegister('contact-tile')
class ContactTile extends PolymerElement {
factory ContactTile(Contact contact, Element parent) {
ContactTile tile = new Element.tag('contact-tile');
tile.contact = contact;
return tile;
}

ContactTile.created() : super.created();

@property Contact contact;
}

child html:
<dom-module id="contact-tile">
<style>
:host {
display: block;
}
</style>

<template>
<div>{{ contact.contactId }}</div>
<div>{{ contact.firstName }}</div>
<div>{{ contact.surname }}</div>
</template>
</dom-module>

联系类别:
class Contact {
String contactId;
String firstName;
String surname;

Contact(Map map) {
contactId = map['contactId'];
firstName = map['firstName'];
surname = map['surname'];
}
}

由于某种原因,在构造函数上更新联系人后,数据绑定(bind)不会在 Web 界面上显示联系人的任何属性。有人能帮我解决这个问题吗?我在 Polymer 0.5 上做过很多次,但在 Polymer 1 上这不起作用。

非常感谢你。

===============================

解决方案

该问题与未在 ContactTile 的构造函数上引发的通知有关。我可以通过使用更新属性并通知的“设置”功能修改联系人来解决此问题。有两种方法可以做到这一点:

一种。 @Property(通知:真)
@PolymerRegister('contact-tile')
class ContactTile extends PolymerElement {
factory ContactTile(Contact contact, Element parent) {
ContactTile tile = new Element.tag('contact-tile');
tile.contact = contact;
return tile;
}

ContactTile.created() : super.created();

@Property(notify: true)
Contact contact;
}

湾。 polymerElement.set("名称", 值);
@PolymerRegister('contact-tile')
class ContactTile extends PolymerElement {
factory ContactTile(Contact contact, Element parent) {
ContactTile tile = new Element.tag('contact-tile');
// tile.contact = contact;
tile.set("contact", contact);
return tile;
}

ContactTile.created() : super.created();

@property
Contact contact;
}

同样为了访问对象属性,我必须使类扩展 JsProxy 并将 @property 添加到每个属性(或 @reflectable 用于方法)。
import 'package:polymer/polymer.dart';

class Contact extends JsProxy {
@property
String contactId;
@property
String firstName;
@property
String surname;

Contact(Map map) {
contactId = map['contactId'];
firstName = map['firstName'];
surname = map['surname'];
}
}

最佳答案

由于Contact类不是一个完整的自定义元素,它应该从 JsProxy 继承吗?并有属性的注释。这将使属性可以在 html 中访问。如下图所示。

class Contact extends JsProxy {
@property String contactId;
@property String firstName;
@property String surname;

Contact(Map map) {
contactId = map['contactId'];
firstName = map['firstName'];
surname = map['surname'];
}
}

关于dart - 数据绑定(bind)结构化对象在我的 polymer 元素上没有显示任何变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40770957/

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