我计划在我当前的 React-Native 项目中使用 WebView,它将渲染一个包含一些图片和外部 CSS 文件的 HTML 页面。我发现的其他解决方案使用了 React-Native 中定义的现有字符串。
我的第一部分工作得很好:
import { WebView } from 'react-native-webview';
import htmlPage from './myPage.html' // same dir for simplicity
Run Code Online (Sandbox Code Playgroud)
我的组件:
render(){
return (
<WebView
source={htmlPage}
/>
)
};
Run Code Online (Sandbox Code Playgroud)
我的 HTML 页面不包含任何指向其相应 CSS 文件的链接,而是在其他地方定义的(不是这里的问题)。我的方法是:
在 WebView 中每个参数包含样式(不支持,仅适用于 JS)
将 CSS 内容作为字符串读取并将其粘贴到 HTML 页面中,然后是 WebView(使用修改后的 HTML 页面)
第二种方法似乎很有希望,遗憾的是以下方法不起作用:
import cssPage from '../someCSS.css'
Run Code Online (Sandbox Code Playgroud)
因为它的结果是:
Unable to resolve module `./someCSS.css` from `......./components/ComponentX/ComponentX.js`: The module `......./someCSS.css` could not be found from `......./components/ComponentX/ComponentX.js`. Indeed, none of these files exist: …Run Code Online (Sandbox Code Playgroud)