reactjs - 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;
...
}
...
解决方案
在 WebView 中,您可以注入如下脚本
<WebView
source={{ uri: this.url }}
ref='WEBVIEW_REF'
injectJavaScript={
"window.testMessage = 'hello world'"
}
/>
Web 视图文档
/**
* Executes the JavaScript string.
*/
injectJavaScript: (script: string) => void;
推荐阅读
- c# - OnEntry 方法不起作用,当使用 postsharp 进行 AOP 时
- java - 在 Spring 中管理(检查和恢复)自定义背景例程的标准方法是什么?
- java - Java 运算符 && 与 || 的求值顺序
- javascript - 如何在按下按钮时在屏幕中间打开一个组件?
- html - 通过 svg 线连接部分(响应式)并在滚动上绘制
- javascript - 内容安全策略在 iPhone 上不起作用
- api - 颤动的http post请求显示状态码500,尽管它在邮递员中工作
- java - 将时区字符串日期解析为 Java 的 Instant
- node.js - 我如何将(例如!lang en)保存到 json 文件中,以便机器人使用该 lang 的差异命令
- rest - 如何使用 apollo 客户端进行休息发布请求?