javascript - 使用weex.registerModule注册module,如何注册到全局?
巴扎黑
巴扎黑 2017-04-11 12:22:40
[JavaScript讨论组]

期望注册一个module,像访问 modal那样在需要的地方直接weex.requireModule('modal')使用。按照下边的代码运行,点击按钮的时候会提示找不到fetch属性。

Uncaught TypeError: Cannot read property 'fetch' of undefined

weex -v
   v1.0.3

我的文件目录
|--app.js
|--src
|----test.vue
|----nativeApi.js
...

app.js是入口文件

import nativeApi from './src/nativeApi.js'
//注册自定义组件
weex.install(nativeApi);

import foo from './src/test.vue'
foo.el = '#root'
export default new Vue(foo);

nativeApi.js

const api = {
    fetch:function(options,callback){
        console.log('fetch called');
    }
};

export default {
    init : function(weex){
        console.log('api init');
        weex.registerModule('api',api);
    }
};

    

test.vue



  

补充:
页面加载后,通过控制台输入以下代码
weex.requireModule('api').fetch();
// 输出 'fetch called'
由此是否可以推断,weex.requireModule的时机有问题?

巴扎黑
巴扎黑

全部回复(1)
PHPz

新版的weex好像已经不使用weex.install(nativeApi);注册module了?,我分享给你新版我是如何注册module的吧

import Vue from 'vue'
window.Vue = Vue

/**
 * 注册component
 * 
 */

// import TestComponent from './components/test/test.vue'
// Vue.component('test-component', TestComponent)


/**
 * 注册module
 * 
 */
require('weex-vue-render')

// shopBase
import shopBase from './modules/shop-base'
window.weex.registerModule('shopBase', shopBase)
'use strict'

const shopBase = {


  setTitle: function (title) {
    try {
      title = decodeURIComponent(title)
    }
    catch (e) {}
    document.title = title
  }

}


export default shopBase

这个文件编译得到的脚本就有全局可以使用的module

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号