NativeScript WebView在src文档中加载本地资源 [英] NativeScript WebView loading local resources in src document

查看:94
本文介绍了NativeScript WebView在src文档中加载本地资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加载本地html文件作为NativeScript WebView组件的src. html文件中包含脚本标记,这些脚本标记引用了也是本地资源(捆绑在应用程序中)的javascript文件. html文件可以很好地加载到WebView中,但是引用的脚本文件(mylib.js)不能.

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.

我的项目实际上是NativeScript-Vue项目,如下所示:

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/**/*" }, 
...

推荐答案

这是已知问题(适用于iOS).您可以尝试使用修补程序,我已经实现了相同的方法在游乐场中,适用于类似问题也适用于Vue.

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.

这篇关于NativeScript WebView在src文档中加载本地资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆