Skip to content

Vue和安卓方法互调

一、Vue调用安卓封装好的方法

直接用window.android.方法名

js
    window.android.takePhoto(params);

二、安卓调用Vue的方法

Vue的方法要挂载到window上

vue
    mounted() {
        // 将要给原生调用的方法挂载到 window 上面
        // window.callJsFunction中的callJsFunction是安卓调用的名称
        window.callJsFunction = this.callJsFunction
    },
    data() {
        return {
            msg: "哈哈"
        }
    },
    methods: {
        callJsFunction(str) {
            this.msg = "我通过原生方法改变了文字" + str
            return "js调用成功"
        }
    }

安卓代码

java
    @Override
    public void callVueJS() {
        tbsWebView.post(new Runnable() {
            @Override
            public void run() {
                webView.loadUrl("javascript:callJsFunction('soloname')");
            }
        });
    }

如有转载或 CV 的请标注本站原文地址