【问题标题】:How to pass dynamic param to function called through injectJavaScript in react-native-webview如何在 react-native-webview 中将动态参数传递给通过 injectJavaScript 调用的函数
【发布时间】:2020-08-22 18:35:09
【问题描述】:

我正在使用 react-native-webview 在 webview 中加载 url。我只是想使用 webview 从本机应用程序调用前端功能以获得某些功能。

this.webView.ref.injectJavaScript('window.chatComponentService.openChat()')

使用静态参数可以正常工作

this.webView.ref.injectJavaScript('window.chatComponentService.openChat("anyValue")')

但是如果我想传递动态值,比如:

this.webView.ref.injectJavaScript('window.chatComponentService.openChat(&{someParam})')

我应该怎么做才能在函数调用中传递动态参数?

【问题讨论】:

    标签: react-native react-native-webview


    【解决方案1】:

    您可以在发送参数之前对参数进行 JSON.stringify。我通常做的是

    const message = JSON.stringify({
        x: 'value1',
        y: 'value2',
        z: 'value3'
    });
    
    this.webView.ref.injectJavaScript(`window.chatComponentService.openChat(${message})`)
    

    【讨论】:

    • 我也试过这个,但没有工作,并通过“错误评估注入的 JavaScript”警告
    • 这里起作用的是:this.webView.ref.injectJavaScript('window.chatComponentService.openChat(' + someParam + ')') 如果 someParam 不是字符串。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    相关资源
    最近更新 更多