react-native android手机webview加载html不显示

最近遇到个问题就是webview引用的html做的地图,但是Android上面不显示页面:

    原因:Android 需要先把静态资源放到 android/app/src/main/assets 目录下面(要自己新建assets目录),

            然后把 require('./base/ImportantMap/map.html')换成 {uri: 'file:///android_asset/base/ImportantMap/map.html'}

import React, { Component } from 'react';
import { WebView, View, StyleSheet } from 'react-native';

export default class App extends Component {
    constructor(props){
        super(props);
        this.state = {
            webViewData:this.props.data
        }
    }
    //组件更新结束之后执行,在初始化render时不执行
    componentDidUpdate(){
        this._sendMessage();
    }
    componentWillReceiveProps(nextProps) {
        if(nextProps.option !== this.props.option) {
            this._sendMessage();
        }
    }
    _handleMessage(e) {
        this.setState({webViewData: e.nativeEvent.data}); //从html接收数据
    }
    _sendMessage() {
        this.refs.charts.postMessage(JSON.stringify(this.props.option)); //发送数据到html页面
    }
    render() {
        return (
            <View style={{flex: 1, height: this.props.height || 400,}}>
                <WebView
                    ref="charts"
                    scrollEnabled = {false}
                    style={{
                        height: this.props.height || 400,
                        backgroundColor: this.props.backgroundColor || 'transparent'
                    }}
                    scalesPageToFit={false}
                    source={{ uri: 'file:///android_asset/base/ImportantMap/map.html'}}
                    onMessage={()=> this._handleMessage()}
                    />
            </View>
        );
    }
}

附上html接收数据:

 window.onload = function(){
        document.addEventListener('message', function (e) { //监听webview的message事件
           // document.getElementById('main').textContent=e.data;
            var message = JSON.parse(e.data);}}

再附上assets下的页面截图;



就可以啦!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值