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

问题描述 投票:1回答:1

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

enter image description here

而且我简化了这样的代码,但是它也不起作用。

  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)}
      />
    );
  };

对不起的语法不好。

javascript reactjs react-native react-native-android react-native-webview
1个回答
0
投票

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

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

将您的供应商文件夹复制到android的资产文件夹(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

© www.soinside.com 2019 - 2024. All rights reserved.