Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

javascript - is it impossible to call a vue js funtion from android webview?

I know this question exists in many site, so I followed that answers but now worked. So I ask this question, I use vuejs using typescript, and webview in android. What I want to do is that First, When I click the button in android app, then call the vuejs function. And Second is calling from js to android. But both are not working. I checked about all possible solutions but not working for me. please let me know how to solve this. I added codes. The url path is right and I can see the page in webview, Home is routed to "http://url/"

Home js

<template>
  <div class="home">
        <h1>This is an Home page</h1>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
export default Vue.extend({
    name: 'Home',
    methods:{
      getAlert(){
        alert(11111);        
      },
      getWebViewAlert(toast:String){
        android.showToast(toast);
      }

    }
})
interface WebAppInterface{  // eslint-disable-line no-unused-vars
  showToast(toast: String) : void;
}

declare var android: WebAppInterface;// eslint-disable-line no-unused-vars

</script>

MainActivity.kt

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        val webviewSetting = binding.webview.settings
        webviewSetting.javaScriptEnabled = true
        webviewSetting.setSupportMultipleWindows(false)
        webviewSetting.javaScriptCanOpenWindowsAutomatically = false
        webviewSetting.loadWithOverviewMode = true
        webviewSetting.useWideViewPort = true
        webviewSetting.setSupportZoom(false)
        webviewSetting.builtInZoomControls = false
        webviewSetting.layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL
        webviewSetting.cacheMode = WebSettings.LOAD_NO_CACHE
        webviewSetting.domStorageEnabled =true
        binding.webview.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView, url: String) {
                binding.webview.loadUrl("javascript:getAlert()")
            }
        }
        binding.webview.webChromeClient = WebChromeClient()
        //binding.webview.addJavascriptInterface(WebAppInterface(this),"android")
        binding.webview.loadUrl(url)

        binding.button.setOnClickListener {
            binding.webview.loadUrl("javascript:getAlert()")
        }
    }

class WebAppInterface internal constructor(c: Context) {
    var mContext: Context
    @JavascriptInterface
    fun showToast(toast: String) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show()
    }
    init {
        mContext = c
    }
}

Error in android studio

I/chromium: [INFO:CONSOLE(1)] "Uncaught ReferenceError: getAlert is not defined", source: url (1)

updated code

import App from './App.vue'
import './registerServiceWorker'
import router from './router'
import store from './store'
import vuetify from './plugins/vuetify'

Vue.config.productionTip = false
let HomeVue; // eslint-disable-line no-unused-vars
new Vue({
  router,
  store,
  vuetify,
  mounted(){
    HomeVue = this
  },
  methods:{
    getAlert(){
      return this;
    }

  },
  render: h => h(App)
}).$mount('#app')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Probably TypeScript is the problem. Just stick to ES5 and try it again.

Also, have you loaded the Vue JS runtime in the HEAD of your HTML?

Edit: You have to include Vue JS in your index.html ... Refer this: (note the script tag inside head section>

https://github.com/vuejs/vuejs.org/blob/master/src/v2/examples/vue-20-hello-world/index.html

<!DOCTYPE html>
<html>
<head>
  <title>My first Vue app</title>
  <script src="https://unpkg.com/vue"></script>
</head>
<body>
  <div id="app">
    {{ message }}
  </div>

  <script>
    var app = new Vue({
      el: '#app',
      data: {
        message: 'Hello Vue!'
      }
    })
  </script>
</body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...