组件是异步实例化的。您可能想尝试此实现。
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script>
<![CDATA[
private var _message:String, _messageChanged:Boolean;
public function get message():String {
return _message;
}
[Bindable("messageChanged")]
public function set message(value:String):void {
if (_message == value) {
return;
}
_message = value;
_messageChanged = true;
invalidateProperties();
dispatchEvent(new Event('messageChanged'));
}
override protected function commitProperties():void {
super.commitProperties();
if (_messageChanged) {
_messageChanged = false;
label1.text = _message;
}
}
]]>
</fx:Script>
<s:Label id="label1"/>
</s:View>
这是基于 Flex 组件生命周期的,它可以让你输入一点,但是如果你不能/不想像这个例子中那样使用数据绑定,那么使用模板也可以:
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script>
[Bindable]
private var _message:String;
public function setMsg(msg:String):void
{
_message = msg;
}
</fx:Script>
<s:Label id="label1" text="{_message}"/>
</s:View>
这个例子可能也是我一个不错的选择 - 取决于您的需要。