更新一个 var,它从另一个组件中获取它的值 [英] Update a var, which get's it it's value from another component
问题描述
我在更新 var 并在创建完成时使用它触发函数时遇到问题.
I'm having trouble with updating a var and firing a function with it on creationcomplete.
在主要组件中,我声明了一个 var 艺术家.这个 var 肯定有效并且有价值.我还需要另一个组件中的 var:一个配置文件组件.我可以通过以下方式获得该变量:
In the main component i declared a var artist. This var certainly works and has a value. I also need that var in another component: a profile component. I am able to get that var with:
newVar = Maincomp(this.parentApplication).artist;
我在配置文件组件的 API 中使用了 newVar.当我使用 mouseEvent 启动 API 函数时,它可以工作.但是,我想在 creationComplete 上启动 API 函数,而 var 不起作用:它返回 Null.我希望使所有涉及的变量都可绑定会有所帮助,但事实并非如此.如何解决这个问题?
I use the newVar in an API in the profile component. When i launch the API function with a mouseEvent, it works. However, i want to launch the API function on creationComplete and than the var doesn't work: it returns Null. I hoped that making all the involved vars bindable would help, but it doesn't. How can this be fixed?
使我的案例更清晰的额外代码:
Extra code to make my case clearer:
在主曲中:
public function handleclick(cName:String):void
{
vName = "aVariableIGetFromAnAPI";
}
在配置文件中:
public function zoekImages(event:FlexEvent):void
{
var api_URL:String = 'http://ws.audioscrobbler.com/2.0/';
var api_imagemethod:String = 'artist.getImages';
var api_key:String = '99f1f497c0bd598f1ec20571a38e2219';
artistImage = DifficultierAPI(this.parentApplication).vName;
var autocorrect:String = '1';
var limit:String = '4';
api_imagerequest = api_URL + '?method=' + api_imagemethod + '&artist=' + artistImage + '&autocorrect=' + autocorrect + '&api_key=' + api_key + '&limit=' + limit;
imageRequest.send(); // hier maak je connectie met lastfm
}
此代码有效,但重点是,如果我使用 MouseEvent 调用此代码,则此代码有效,但在创建完成时需要启动时无效.或者,它返回 Null 而不是值.我认为使涉及的变量可绑定会解决它,但事实并非如此.有什么帮助吗?
This code works, but point is that this code works if i call it with a MouseEvent, but doesn't when it needs to launch on creationcomplete. Or well, it returns Null instead of a value. I thought that making the involved vars bindable would fix it, but it doesn't. Any help?
推荐答案
如果您希望将变量从主应用程序一直绑定到嵌套组件,只需在您的组件中将变量声明为 public 且可绑定:
If you're looking to bind the variable all along from the main application to the nested component, simply declare a variable as public and bindable within your component :
主要应用
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:Components="Components.*">
<fx:Script>
<![CDATA[
[Bindable]
public var myMainVar:String;
protected function button1_clickHandler(event:MouseEvent):void
{
myMainVar = "Hi there!";
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:Button label="Click me to update the coponent's variable" click="button1_clickHandler(event)"/>
<Components:MyCustomComponent myComponentVar="{myMainVar}"/>
</s:WindowedApplication>
嵌套组件
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
[Bindable]
public var myComponentVar:String;
]]>
</fx:Script>
<s:Label text="{myComponentVar}"/>
</s:Group>
我希望这就是你要找的 :-)
I hope it is what you're looking for :-)
这篇关于更新一个 var,它从另一个组件中获取它的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!