为了账号安全,请及时绑定邮箱和手机立即绑定

react-native-webview 加载本地 HTML 文件但得到纯文本

react-native-webview 加载本地 HTML 文件但得到纯文本

FFIVE 2022-06-16 10:50:22
我尝试加载 HTML 文件取决于其他 .js 文件。所以我写了这样的代码,但是在 webview 中得到了纯文本。render = () => {    let source;    if (__DEV__) {      source = require('./vendor/Editor/index.html');    } else {      source =        Platform.OS === 'ios'          ? require('./vendor/Editor/index.html')          : {uri: 'file:///android_asset/vendor/Editor/index.html'};    }    return (      <WebView        startInLoadingState        source={source}        onMessage={this.handleMessage}        automaticallyAdjustContentInsets={false}        style={[          AppStyles.container,          styles.container,          {height: this.state.visibleHeight},        ]}      />    );  };我像这样简化代码,但它也不起作用。  render = () => {    setTimeout(() => {      this.webview.injectJavaScript(        'window.ReactNativeWebView.postMessage(document.body.innerHTML)',      );    }, 3000);    return (      <WebView        source={require('./vendor/GrEditor/index.html')}        onMessage={e => console.log('e: ', e)}      />    );  };对不起语法不好。
查看完整描述

1 回答

?
料青山看我应如是

TA贡献1772条经验 获得超8个赞

要在 android 中加载本地 html 文件,即使在开发模式下也需要使用 android 资产路径。所以它会是这样的:

let source = Platform.OS === 'ios'
          ? require('./vendor/Editor/index.html')
          : {uri: 'file:///android_asset/vendor/Editor/index.html'};

将您的供应商文件夹复制到 android.assets 文件夹(project_folder\android\app\src\main\assets)中。
这是供您参考的链接:https ://github.com/react-native-community/react-native-webview/blob/master/docs/Guide.md#loading-local-html-files


查看完整回答
反对 回复 2022-06-16
  • 1 回答
  • 0 关注
  • 372 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号