EEUI.APP EEUI.APP 首页 文档 - 指南 - 组件 - 模块 插件 - 插件市场 - 开发文档 热更新 控制台 Editor 例子 社区 GitHub 首页 文档 - 指南 - 组件 - 模块 插件 - 插件市场 - 开发文档 热更新 控制台 Editor 例子 社区 GitHub
    ## # 概述 v0.9.5+ JSService 和 Weex 实例在 JS Runtime 中并行运行。Weex 实例的生命周期可调用 JSService 生命周期。目前提供创建、刷新、销毁生命周期。 重要提醒: JSService 使用不当会导致内存增高或全局污染! ## # 注册 ### # iOS
    1. [WXSDKEngine registerService:@"SERVICE_NAME" withScript: @"SERVICE_JS_CODE" withOptions: @{}];
    2. // or
    3. [WXSDKEngine registerService:@"SERVICE_NAME" serviceScriptUrl: @"SERVICE_JS_URL" withOptions: @{}];
    ### # Android
    1. HashMap<String, String> options = new HashMap<>()
    2. options.put("k1", "v1")
    3. String SERVICE_NAME = "SERVICE_NAME"
    4. String SERVICE_JS_CODE = "SERVICE_JS_CODE"
    5. boolean result = WXSDKEngine.registerService(SERVICE_NAME, SERVICE_JS_CODE, options)

    params of options Could have { create, refresh, destroy } lifecycle methods. In create method it should return an object of what variables or classes would be injected into the Weex instance.

    ### # Web
    1. <script src="SERVICE_JS_CODE_URL"></script>
    ## # 样例
    1. service.register(SERVICE_NAME / same string with native /, {
    2. /
    3. JSService lifecycle. JSService create will before then each instance lifecycle create. The return param instance is Weex protected param. This object will return to instance global. Other params will in the services at instance.
    4. @param {String} id instance id
    5. @param {Object} env device environment
    6. @return {Object}
    7. /
    8. create: function(id, env, config) {
    9. return {
    10. instance: {
    11. InstanceService: function(weex) {
    12. var modal = app.requireModule('modal')
    13. return {
    14. toast: function(title) {
    15. modal.toast({ message: title })
    16. }
    17. }
    18. }
    19. },
    20. NormalService: function(weex) {
    21. var modal = app.requireModule('modal')
    22. return {
    23. toast: function(title) {
    24. modal.toast({ message: title })
    25. }
    26. }
    27. }
    28. }
    29. },
    30. /
    31. JSService lifecycle. JSService refresh will before then each instance lifecycle refresh. If you want to reset variable or something on instance refresh.
    32. @param {String} id instance id
    33. @param {Object} env device environment
    34. /
    35. refresh: function(id, env, config){
    36. },
    37. /**
    38. JSService lifecycle. JSService destroy will before then each instance lifecycle destroy. You can deleted variable here. If you doesn't detete variable define in JSService. The variable will always in the js runtime. It's would be memory leak risk.
    39. @param {String} id instance id
    40. @param {Object} env device environment
    41. @return {Object}
    42. */
    43. destroy: function(id, env) {
    44. }
    45. })
    Use JSService
    1. <script>
    2. var _InstanceService = new InstanceService(weex)
    3. var _NormalService = new service.NormalService(weex)
    4. module.exports = {
    5. created: fucntion() {
    6. // called modal module to toast something
    7. _InstanceService.toast('Instance JSService')
    8. _NormalService.toast('Normal JSService')
    9. }
    10. }
    11. </script>
    在 GitHub 上编辑此页

    最后一次更新: 6/7/2019, 8:03:58 AM