从设备本地文件系统响应本机 webview 加载 [英] react native webview load from device local file system
问题描述
我正在尝试将设备本地文件系统中的 .html
、.js
、.css
文件加载到 React Native WebView 组件中.我能够获得 index.html
文件的路径,但将其指定为 WebView 的 url 只会引发错误.我该怎么办?请帮忙!
I'm trying to load .html
, .js
, .css
files from device's local file system into React Native WebView component. I was able to get the path to the index.html
file but specifying this as the url for the WebView simply throws an error. How would I go about this? Please help!
推荐答案
在 React Native 中,现在可以要求一个 HTML 文件并将其用作像这样的 web 视图的源(路径是相对于 react-您在其中使用它的本机文件):
In react native it is now possible to require an HTML file and use it as the source for the web view like this (the path is relative to the react-native file you use it in):
const webapp = require('./webapp/index.html');
然后像这样在 WebView 中使用它:
and the use it in the WebView like this:
<WebView source={webapp} />
遗憾的是,这不会加载 HTML 中引用的 CSS 和 JavaScript 文件.一个解决方案可能是,内联编写所有 CSS 和 JS(例如,通过使用构建过程).
Unfortunately this does not load CSS and JavaScript files, that are referenced in the HTML. A solution could be, to write all the CSS and JS inline (e.g. by using a build process).
这篇关于从设备本地文件系统响应本机 webview 加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!