How to inciude a local html file in a webview in nativescript main-page.js?

廉价感情. 提交于 2020-05-17 07:03:05

问题


Is it possible to add a local html file in the nativescript webview ? If yes How can I do it using javascript ? When I add online page it works , I can add www.google.com in the webview it works .But I want to add a local page but I don't find a way to do this .


回答1:


Yes, it's possible. You need to consider that all NativeScript apps are build by default with Webpack and the default webpack.config.js will take care of certain files (like the ones in a fonts folder or like all images with *.png and *..jpg extensions). The webpack build will bundle all JavaScript files and in the case of the Angular flavor will also cognitively include the Angular related HTML files. However, the default webpack.config.js won't "know" about your custom HTML file.

The solution is to let Webpack know that it should copy the specific HTML file. This should be done via the CopyWebpackPlugin section in webpack.config.js file.

Example (assuming we have a file called test.html in the app directory)

    new CopyWebpackPlugin([
        { from: { glob: "test.html" } }, // HERE
        { from: { glob: "fonts/**" } },
        { from: { glob: "**/*.jpg" } },
        { from: { glob: "**/*.png" } },
    ], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),

For real-life example see this config where this HTML file is the one we are targeting.



来源:https://stackoverflow.com/questions/60624570/how-to-inciude-a-local-html-file-in-a-webview-in-nativescript-main-page-js

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