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

Des*_*hen 6 javascript reactjs react-native react-native-android react-native-webview

我尝试加载一个依赖于其他 .js 文件的 HTML 文件。

所以我写了这样的代码,但在 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},
        ]}
      />
    );
  };
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我像这样简化了代码,但它也不起作用。

  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)}
      />
    );
  };
Run Code Online (Sandbox Code Playgroud)

抱歉语法不好。

Ank*_*ana 10

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

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

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