javascript - Nativescript 裸 webview 似乎不起作用

标签 javascript android nativescript

我最近开始使用 nativescript,但我遇到了一个似乎无法解决的问题。

我想要完成的是只打开一个基本的 WebView 并设置一个外部 url 来加载,例如 stackoverflow.com。

我正在使用 Android api 17 的模拟器上运行它

app.js

var application = require("application");
application.mainModule = "main-page";
application.cssFile = "./app.css";
application.start();

ma​​in-page.js

var vmModule = require("./main-view-model");
var webViewModule = require("ui/web-view");
var webView = new webViewModule.WebView();

function pageLoaded(args) {
    var page = args.object;
    var web = page.getViewById(webViewModule.WebView,"webView");
    web.url="http://google.com";

}
exports.pageLoaded = pageLoaded;

ma​​in-page.xml

<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="pageLoaded">
  <StackLayout>
    <WebView id="webView" colSpan="4" row="2"/>
  </StackLayout>
</Page>

我还检查了 INTERNET 权限是否设置为应用程序,是的,它已启用。

应用程序也停止工作,命令

tns 运行 android --emulator

在模拟器上成功安装并运行应用程序后,它只是垃圾邮件并输出大量内容。

你能帮我理解到底应该如何工作吗?

最佳答案

tl;dr for others having problems with WebView

出于某种原因您不能将 WebView 放入 StackLayout,它根本不会显示。请改用 GridLayout。有一个 GitHub issue对此。

完整答案

Android 模拟器确实很啰嗦,但在该日志中的某处(很可能在最后)几乎可以肯定有错误的堆栈跟踪。如果您运行的是 Mac,iOS 模拟器就不那么繁琐了。

也就是说,让我们修复您的代码。下面是工作代码,注释以显示对代码的更改。

app.js

没问题,看起来应该是这样!

ma​​in-page.js

/**
 * Do you actually have a file called 'main-view-model.js' in the same
 * folder as this file? In any case, it's not used in your example code
 * so I commented it out.
 */
//var vmModule = require("./main-view-model");
var webViewModule = require("ui/web-view");

/**
 * Here you are creating a NEW webview. If you want to, you can create
 * element dynamically and then attach them to the view. However, in your
 * example you already have a webview in your xml file so there's no need
 * to create a new one.
 */
//var webView = new webViewModule.WebView();

function pageLoaded(args) {
    var page = args.object;
    /**
     * Corrected the line below. What you're doing here is pretty
     * much equal to $('#webView') in jQuery. You're selecting
     * an element
     */
    var web = page.getViewById("webView");
    //var web = page.getViewById(webViewModule.WebView,"webView");
    web.url = "http://google.com";

}
exports.pageLoaded = pageLoaded;

ma​​in-page.xml

<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="pageLoaded">
    <GridLayout>
        <WebView id="webView" />
    </GridLayout>
</Page>

所以我在这里做了一些事情。首先,我删除了 colSpan="4"row="2"。这两个参数仅用于 GridLayouts ,并且您正在使用 StackLayout。 但是如您所见,我已将您的 StackLayout 更改为 GridLayout。这样做的原因是由于某种原因你不能将 WebView 放在 StackLayout 中,它根本不显示。有一个 GitHub issue对此。

替代方案

如果您只想创建一个显示 Google 的 WebView,您可以直接在 XML 中设置 url 属性。如果您这样做,您甚至不需要 Javascript。当然,如果您想动态设置 url,您需要从 Javascript 中进行设置。

设置url属性的例子:

<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="pageLoaded">
    <GridLayout>
        <WebView id="webView" url="http://www.google.com" />
    </GridLayout>
</Page>

关于javascript - Nativescript 裸 webview 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30482269/

相关文章:

android - android 中的 tabhost 内的 fragment 是否有任何 "on leave tab "事件?

android - Linux内核wait_for_completion_timeout没有被完成唤醒

nativescript - 在 Nativescript 代码共享项目中使用 Angular 库

android - NativeScript:字体系列未在 Android 上加载

javascript - ng-repeat 不适用于表 <tr> 但适用于列表 <li>

javascript - knockout js - 单击 foreach 语句下的列表时如何更改列表颜色?

javascript - AngularJS : ng-model doesn't update correctly with ng-keydown. ng-model 关闭一个字符

javascript - 将元素悬停在图表上

android - 是否可以从 Android API 随机访问写入 Google Drive 文件?

javascript - 如何有条件地隐藏 native 脚本( Angular )中的选项卡项目?