1

我试图从外部文件中调用一个纯 JavaScript 函数,但它没有运行。请提出一种注入普通javascript函数的方法。

我曾尝试使用 WebView:

<View style={{flex: 1}}>
    <WebView ref={ref => { this.webview = ref; }}
        source={{ html: HTML }}
        injectedJavaScript={jsCode}
        javaScriptEnabledAndroid={true}
        javaScriptEnabled={true}
    >
    </WebView>
</View>
4

2 回答 2

0
module.exports.Example = (params) => { alert("123456") }

接着

import {Example} from 'fileName.js'

如果它的 index.js 文件只指定文件夹的路径

于 2020-11-03T11:50:33.637 回答
0

创建一个外部 .js 文件,在那里编写您的代码。

module.exports.yourFuncName = (params) => {
     // your function def
} 

OR

module.exports.yourFuncName = function (params){
     // your function def
} 

在您的组件中只需导入。

import { yourFuncName } from "location of file in code";

let jsCode = `
 yourFuncName(params)
`;

<View style={{flex: 1}}>
<WebView ref={ref => { this.webview = ref; }}
    source={{ html: HTML }}
    injectedJavaScript={jsCode}
    javaScriptEnabledAndroid={true}
    javaScriptEnabled={true}
>
</WebView>
于 2020-11-03T08:11:21.153 回答