在我的 react-native 应用程序中,我想显示一个 webview,它加载本地 html5 web 应用程序(单页应用程序,SPA)。
我能够加载一个 html 文件,它位于相对文件夹 webapp
中,使用
var webapp = require('./webapp/index.html');
和
<WebView source={webapp}></WebView>
正如我在本帖中所述:react native webview load from device local file system
不幸的是,这个本地网络应用程序的 Assets ,如 .js 和 .css 文件,没有被网络应用程序加载。
在 react-native 应用程序的开发过程中,这些文件可通过开发服务器获得。但是,当应用程序在 iOS 设备上打包并运行时, Assets 不可用(我在 Safari 开发人员工具中收到 404 错误)。
我如何确保 webapp
文件夹中的所有 Assets (JS、CSS 等)在本地打包的应用程序中可用。
最佳答案
By setting the baseURL, the WebView knows where to look for the css/js that you link to
喜欢:
<WebView baseURL="/webApp" html={html} />
在github上查看问题的答案:
https://github.com/facebook/react-native/issues/1442#issuecomment-106906969
或阅读 nick-weaver 详细回答的示例: Loading javascript into a UIWebView from resources
关于javascript - 将本地网络应用程序包含到 native webview 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35851788/