我无法让baseUrl参数在react-native-webview中工作。我的项目结构如下:
root
---> _tests _
---> android
-------> web
---> ios
---> node_modules
---> src
-------> components
------------> web
------------> MyComponent.js
---> web 我已经插入了3次web文件夹,如图所示(实际上只需要插入一次,这只是为了测试)。每个“web”文件夹都包含ponies.jpg。然而,React Native并没有接收到任何东西。我只得到了4个损坏的图像与alt (即‘'Ponies')显示。我也尝试过使用baseUrl:'web/‘,但没有效果。以下是MyComponent.js中的代码:
import React from 'react';
import {View, Text, StyleSheet, Image} from "react-native";
import {WebView} from "react-native-webview";
var html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<img src="ponies.jpg" alt="Ponies" height="42" width="42">
<img src="ponies.jpeg" alt="Ponies" height="42" width="42">
<img src="./ponies.jpg" alt="Ponies" height="42" width="42">
<img src="./ponies.jpeg" alt="Ponies" height="42" width="42">
</body>
</html>
`;
export default class MyComponent extends React.Component {
render() {
return (
<View style = {styles.container}>
<WebView
style = {styles.webview}
source = {{html: html, baseUrl: 'web/'}}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
height: 350,
width: 350
},
webview: {
height: 350,
}
})谢谢!
发布于 2019-02-13 18:09:39
好的,我为android找到了一个解决方案/解决方法:我实际上已经在另一个答案中看到了这一点,但我将稍微扩展一下:
主资产需要是'file:///android_asset/'.
发布于 2019-02-13 02:08:35
当前,您的web文件夹位于android文件夹中
添加文件夹web并使用此结构:
app/web/ponies.jpg使用web/作为基本URL。
难题的最后一部分是将web文件夹添加到XCode项目中(在XCode中完成此操作)。否则,web文件夹中的文件不会包含在为ios设备构建的捆绑包中。
注意:其中app是应用程序的根目录。
https://stackoverflow.com/questions/54655504
复制相似问题