NativeScript WebView loading local resources in src document

穿精又带淫゛_ 提交于 2019-12-02 12:13:09

问题


I am loading a local html file as the src for a NativeScript WebView component. Contained within the html file are script tags which reference javascript files that are also local resources (bundled within the app). The html file loads into the WebView just fine, but the referenced script file (mylib.js) does not.

I suspect a pathing problem but I have tried almost every variation I can think of to no avail.

My project is actually a NativeScript-Vue project and is as follows:

App.vue

<template>
    <Page @loaded="onPageLoaded">
        <ActionBar title="Welcome to WebView"/>
        <GridLayout>
            <WebView ref="myWebView" row="0" col="0" 
            :src="filePath" @loadFinished="onWebViewLoaded" />
        </GridLayout>
    </Page>
</template>

<script>
import * as fs from "tns-core-modules/file-system"
import * as utils from "utils/utils"

  export default {
    data() {
      return {
        filePath: ''
      }
    },
    methods: {
        onPageLoaded () {
            this.setLocalIndexFilePath()
        },
        onWebViewLoaded (event) {
            if (event.error) { 
                console.log(error)
            } else {
                console.log('webview loaded')
            }
        },
        setLocalIndexFilePath () {
            const deviceName = 
            utils.ios.getter(UIDevice, UIDevice.currentDevice).name
            // iPhone 6 is the name of my simulator
            if (deviceName == 'iPhone 6') {
                const webViewSRC = 
                encodeURI(`${fs.knownFolders.currentApp().path}/www/index.html`)
                this.filePath =  webViewSRC
                console.log(webViewSRC)
            } else {
                this.filePath = "~/www/index.html"
            }
        }
    }
  }
</script>

index.html

<!doctype html>
<head>
    <script src="./mylib.js" type="text/javascript"></script>
    <script type="text/javascript">
        function onBodyLoaded() {
            var msg = document.getElementById('msg');
            msg.insertAdjacentHTML('beforeend', '<br />body loaded!');
        }

        function onLocalButtonClicked() {
            var msg = document.getElementById('msg');
            msg.insertAdjacentHTML('beforeend', '<br />local: You clicked button!');
        }
    </script>
</head>
<html>
    <body onload="onBodyLoaded()">
      <Button onclick="onLocalButtonClicked()">Click Me</Button>
      <Button onclick="onButtonClicked()">Click Me to test external js</Button>
      <p id="msg">Debug:</p> 
    </body>
</html>

mylib.js

// This function never gets called
function onButtonClicked() {
  var msg = document.getElementById('msg');
  msg.insertAdjacentHTML('beforeend', '<br />external js file: You clicked button!');
}

webpack.config.sys

... 
// Copy assets to out dir. Add your own globs as needed.
new CopyWebpackPlugin([
    { from: "fonts/**" },
    { from: "**/*.+(jpg|png)" },
    { from: "assets/**/*" },
    { from: "www/**/*" }, 
...

回答1:


This is a known issue with iOS. There is a patch work you could try, I had implemented the same in Playground for a similar issue, its applicable for Vue too.



来源:https://stackoverflow.com/questions/53309673/nativescript-webview-loading-local-resources-in-src-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!