首页 > 解决方案 > ReactNativeJS:ReferenceError:找不到变量:文档

问题描述

我使用 react native 开发应用程序,但我试图从我的app.js调用setConf()函数,但是当我导入这个 js 时,React 显然无法识别Script.js文件中的文档,如何调用没有文件问题的功能?谢谢

错误:ReactNativeJS:ReferenceError:找不到变量:文档

import React from 'react';
import {
  StyleSheet,
  Text,
  Button,
  View
} from 'react-native';
import WebView from 'react-native-webview';
import {
  Colors
} from 'react-native/Libraries/NewAppScreen';

import setConf from './Script'

setConf();

const App: () => React$Node = () => {
  return(

    <WebView
        originWhitelist={['*']}
        source={{uri: "file:///android_asset/widget/index.html"}}
        allowFileAccess={true}
        allowUniversalAccessFromFileURLs={true}
      />
  );
};

const styles = StyleSheet.create({
  scrollView: {
    backgroundColor: Colors.lighter,
  },
  engine: {
    position: 'absolute',
    right: 0,
  },
  body: {
    backgroundColor: Colors.white,
  },
  sectionContainer: {
    marginTop: 32,
    paddingHorizontal: 24,
  },
  sectionTitle: {
    fontSize: 24,
    fontWeight: '600',
    color: Colors.black,
  },
  sectionDescription: {
    marginTop: 8,
    fontSize: 18,
    fontWeight: '400',
    color: Colors.dark,
  },
  highlight: {
    fontWeight: '700',
  },
  footer: {
    color: Colors.dark,
    fontSize: 12,
    fontWeight: '600',
    padding: 4,
    paddingRight: 12,
    textAlign: 'right',
  },
});

export default App;

脚本.js

var form = document.querySelector(".form-composer");
form.addEventListener("submit", setData);

export function setConf() {
  var Id = 1;
  ...
}
...

标签: reactjsreact-nativereact-native-android

解决方案


在 WebView 中,您可以注入如下脚本

<WebView
 source={{ uri: this.url }}
 ref='WEBVIEW_REF'
  injectJavaScript={
   "window.testMessage = 'hello world'"
  }
/>

Web 视图文档

/**
 * Executes the JavaScript string.
*/
injectJavaScript: (script: string) => void;

推荐阅读