javascript - 在 React Native 应用程序中调试 WebView

标签 javascript android react-native webview

我有一个使用 WebView 从 Assets 呈现 HTML 页面的 React Native 应用程序。该页面有一些 javascript 进行一些处理。问题是我无法从 Web View 中看到 console.log 语句。我试过 Chrome Remote Remote Debugging WebViews

代码如下所示。请注意,对于 Android,我正在尝试提供一些原生 Prop 来启用调试。

import React from 'react';
import Expo from 'expo';
import { WebView } from 'react-native';

export default class App extends React.Component {

  render() {
    const htmlURL = Expo.Asset.fromModule(require('./assets/index.html')).uri;
    return (
      <WebView nativeConfig={{props: {webContentsDebuggingEnabled: true}}}  
      source={{uri: htmlURL}} />
    );
  }
}


const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

任何关于如何工作的想法都将受到高度赞赏。

最佳答案

在 React Native 中检查 WebView 的最简单方法是使用 Remote JS Debugger.这对于在 iOS 或 Android 中工作具有额外的好处,因为您只需调试在应用程序上运行的 JavaScript。

为了查看 WebView,您需要更进一步并使用 Chrome's Remote Devices .

DevTools Chrome

如果您单击与 index.html 匹配的文档旁边的检查您想要调试,然后您可以在控制台中查看该 WebView 的所有日志。

enter image description here

我添加了 <script> index.html内部在 <header>这只是执行以下操作:

console.log('This is showing in the console!')

您可以在上图中看到,它在正在检查该 WebView 的 DevTools 中注销。

有关 react-native-webview 调试指南的更多信息,请访问 Debugging Guide Docs

关于javascript - 在 React Native 应用程序中调试 WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47711418/

相关文章:

javascript - 使用 Vue Router 时 Pusher 和 Vue.js 组件不会离开 channel

android - 无法访问在 Google Glass 上的应用内拍摄的照片

Android AsyncTask 取消时不会停止,为什么?

reactjs - React Native 如何从另一个文件中调用一个类的函数?

javascript - 改变 JSON 对象的 React 状态 将数组存储为值

ios - 设置调试或发布 | react native - iOS

javascript - 如何发送带有附加元数据的 html,以便 JS(ajax)知道如何处理响应?

javascript - 加载数据时动态向下滚动 DIV 层

javascript - 如何使用 AJAX 将表单选择输入的值传递到 php 文件而不提交?

Android Xamarin : Open file from Storage (with path)