首页 > 解决方案 > 如何在 Nativescript Vue WebView 中启用 LocalStorage?

问题描述

我的 Nativscript Vue 应用程序中有一个WebView,但在 Android 上,嵌入式网站无法访问浏览器中的 localStorage。这个问题解释了如何为 Angular 做这件事。问题是不清楚应该从哪里获取 webview 实例。完整性问题:如何访问包含该android属性的 WebView 实例以启用本地存储?

标签: androidwebviewlocal-storagenativescriptnativescript-vue

解决方案


这就是我最终解决它的方法。我需要访问通过属性中的事件处理程序传递的 WebView event.object。组件上没有调用事件onWebViewLoaded,因为这不是 Angular。相反,我们需要挂钩元素上的loadFinished事件。WebView

<template>
  <Page>
    <WebView
      src="https://www.example.com/"
      @loaded="onLoaded"
    />
  </Page>
</template>

<script>
export default {
  methods: {
    //when the webview finishes loading
    onLoaded(event) {
      //get the webview
      const webView = event.object

      //if there is android
      if (webView.android) {
        //enable dom storage in the webview on android
        webView.android.getSettings().setDomStorageEnabled(true)
      }
    }
  }
}
</script>


推荐阅读