gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:35:51 26 4
gpt4 key购买 nike

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

抱歉语法不好。

最佳答案

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

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

将您的 vendor 文件夹复制到 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

关于javascript - react-native-webview 加载本地 HTML 文件但得到纯文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61072105/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com