The blogger is doing iOS, so define the global method is very understand, there are many ways to do it, such as extension, singleton, PCH, etc., but about WEEX how to do a global method to facilitate the introduction? 1. Set up your WEEX project, this need not say more, really not, go to weex official website to see; 2. Create a new js file: let’s call it function.js and write your method to it:

export function getUrl(bundleUrl,fileName,dir,host){ var nativeBase; var isAndroidAssets = bundleUrl.indexOf('file://assets/') >= 0; var isiOSAssets = bundleUrl.indexOf('file:///') >= 0 && bundleUrl.indexOf('WeexDemo.app') > 0; if (isAndroidAssets) { nativeBase = 'file://assets/'; } else if (isiOSAssets) { nativeBase = bundleUrl.substring(0, bundleUrl.lastIndexOf('/') + 1); } else { host = host||'localhost:8081'; var matches = /\/\/([^\/]+?) \//.exec(bundleUrl); if (matches && matches.length >= 2) { host = matches[1]; } nativeBase = 'http://' + host + '/' + dir + '/'; } var h5Base = './index.html? page=./' + dir + '/'; // in Native var base = nativeBase; if (typeof window === 'object') { base = h5Base; } return base+fileName; }Copy the code

What if you need more than one method? It doesn’t matter, go ahead and add it in the format above:

Export function 1(parameter){} export function 2(parameter){} export function 3(parameter){}Copy the code

Is it easy? }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}

<script> import {getUrl} from "./function"; </script> </script>Copy the code

And then when I use it, say I want to do a local jump:

bigAction(index){
        console.log('will jump')
        navigator.push({
          url: getUrl(weex.config.bundleUrl,'homeBig.js'),
          animated: "true"
        }, event => {
          modal.toast({ message: 'callback: ' + event })
        })
      },
Copy the code

The disadvantage is that every time you use it, you need to introduce it. If you want to have the same effect as PCH in iOS, the blogger has not found it yet. If you know it, you will update it again.