---
title: "[转]Flex模块化的目的及其步骤"
author: "AS3"
date: "2011-10-20T03:35:40Z"
tags: []
categories: ["Web前端"]
slug: "flex"
draft: false
lastmod: "2011-10-20T03:35:40Z"
id: 1202437
url: "https://as3.iteye.com/blog/1202437"
---

[http://developer.51cto.com/art/201008/219089.htm](http://developer.51cto.com/art/201008/219089.htm)

 

Flex模块化还是比较实用的，于是我研究了一下Flex模块化的方法，这里和大家简单分享一下，希望对你的学习有所帮助。

AD：

 

本文和大家重点讨论一下Flex模块化的概念，主要包括Flex模块化的目的和如何进行Flex模块化两大部分内容，相信本文介绍一定会让你有所收获。

**Flex模块化学习总结**

**1.为什么要Flex模块化**

Flex模块化提供了分离应用程序代码到不同的swf文件上以便减少下载时间和文件大小。使用Modules的好处是：

1.主应用程序开始时不需马上下载modules。应用程序会根据需求载入和卸载modules。

2.由于每个modules独立于其他应用程序modules，当需要改变一个modules时，你只需要重编译这个modules而不是整个应用程序。

Flex模块化可创建Flex模块化应用程序是提高Flex框架性能的重要方面，提供更多能力控制下载时间和文件大小，使用modules，你可以根据哪些可被独立载入，哪些可被其他程序使用而分离应用程序代码，

**2.如何Flex模块化**

**2.1创建模块**

基于mxml的模块,在你的工程右键-&gt;new-&gt;mxmlModule

```

<?xmlversionxmlversion="1.0"encoding="utf-8"?> 
 

<mx:Modulexmlns:mxmx:Modulexmlns:mx="http://www.adobe.com/2006/mxml" 


layout="absolute"width="400"height="300"> 

 

</mx:Module> 

 
```

基于as的模块

```

package  
 
{  
importmx.modules.Module;  
 
publicclassGeneralextendsModule  
 
{  
publicfunctionGeneral()  
 
{  
super();  
 
}  
}  
}  
 
```

**2.2Flex模块化中加载模块**

2.2.1使用ModuleLoader加载;

```
<mx:ModuleLoaderurlmx:ModuleLoaderurl="ContactList.swf"/> 
```

2.2.2使用ModuleManager加载

```

PrivatefunctioncreationHandler():void  
 
{  

_moduleInfo=  

 

ModuleManager.getModule('ContactList.swf'  

 
);  
_moduleInfo.addEventListener(ModuleEvent.READY,  
 
moduleLoadHandler);  
 
_moduleInfo.load();  
}  
 
PrivatefunctionmoduleLoadHandler(evt:ModuleEvent):void  
 
{  
 
canvas.addChild(_moduleInfo.factory.create()as  
 
DisplayObject);  
 
}  
 
```

2.2.3创建弹出模块

```

Vargeneral:ModuleLoader  
 

general=ModuleLoader(mx.managers.PopUpManager.  

createPopUp(this,ModuleLoader));  
 

general.url="General.swf";  

```

注意：在创建弹出模块时，有时会产生:无法将mx.managers::PopUpManagerImpl@bba9941转换为mx.managers.IPopUpManager这种错误,这个时候我们需要在主程序里引入

```

importmx.managers.IPopUpManager;  
 
privatevaripopUp:IPopUpManager  
```