AppRegistry

AppRegistry是JS运行所有React Native应用的入口,应用的根组件通过AppRegistry的registerComponent方法来注册自己,这样原生系统就可以加载js代码并在应用启动完成后通过AppRegistry.runApplication来运行应用。

AppRegistry应该在其他模块在引进来之前就引进来。

RN的应用入口文件:index.android.js或者index.ios.js

方法

1:AppRegistry.registerComponent(appKey:string,getComponentFunc:ComponentProvider)

import {AppRegistry} from 'react-native'; import App from './App'; AppRegistry.registerComponent('LeecoBrowser', () => App); 分析:

LeecoBrowser:注册名,和项目名一样 App:引入的模块名

2:AppRegistry.getAppKeys(),获取注册的模块名,返回值是一个模块名的数组

使用:

import {AppRegistry} from 'react-native'; import App from './App'; console.log("before",AppRegistry.getAppKeys()) AppRegistry.registerComponent('LeecoBrowser', () => App); AppRegistry.registerComponent('LeecoBrowser1', () => App); console.log("after",AppRegistry.getAppKeys()) 结果:

'before', [] 'after', [ 'LeecoBrowser','LeecoBrowser1' ]

3:AppRegistry.registerRunnable(appKey:string,func:Function),

应用注册之后回调用该应用,这个方法是由系统调用,以下写法会导致应用起不来 使用:

import {AppRegistry} from 'react-native';

import App from './App'; AppRegistry.registerRunnable('LeecoBrowser', () => { "use strict"; console.log("before") }); AppRegistry.registerComponent('LeecoBrowser', () => App); AppRegistry.registerRunnable('LeecoBrowser', () => { "use strict"; console.log("after") });

结果:

after

4:AppRegistry.registerConfig(config: Array)

5:AppRegistry.runApplication(appKey: string, appParameters: any)

6:AppRegistry.unmountApplicationComponentAtRootTag(rootTag: number)

“结束”一个应用并销毁视图,参数为在runApplication中使用的标签名。它们必须严格匹配。

results matching ""

    No results matching ""