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

陈德斯蒙

我尝试加载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)}
      />
    );
  };

对不起,语法不好。

Ankit makwana

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章