【问题标题】:How to reload page on a Cordova project?如何在 Cordova 项目上重新加载页面?
【发布时间】:2015-12-29 08:14:12
【问题描述】:

我正在构建一个应用程序,使用 polymer starter kitcordova 来包装项目。现在,由于我使用firebase 作为存储数据的数据库,我最终使用了两个原生的firebase javascript 函数来查找用户数据:

getAuth()

var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
var authData = ref.getAuth();
if (authData) {
  console.log("Authenticated user with uid:", authData.uid);
}

onAuth()

var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.onAuth(function(authData) {
  if (authData) {
    console.log("Authenticated with uid:", authData.uid);
  } else {
    console.log("Client unauthenticated.")
  }
});

这两个函数需要重新加载才能从 firebase 中取回数据,但是:

window.location.reload();

没用

Alos 寻找了一个 Cordova 插件:webview-reloader,安装了它,但重定向和重新加载仍然无法正常工作。

当我使用 reload() 函数时,我的安卓手机屏幕变白,应用程序停止工作。需要关闭应用并重新打开。

【问题讨论】:

  • 你试过document.location = 'index.html'吗? stackoverflow.com/a/8506527
  • 确实尝试了 document.location = 'index.html' 并抛出错误,说他找不到文件位置

标签: javascript android cordova firebase


【解决方案1】:

由于 Cordova 是浏览器窗口的包装器:

window.location.reload(true);

它适用于浏览器窗口以及 Cordova 应用程序。

【讨论】:

  • 不,这不会重新加载任何内容 — 无法加载网页并出现错误:帧加载中断
  • 您能否提供一些示例代码来说明为什么它不适合您?
  • 这里是一个例子——window.location.reload()
  • 如果您使用诸如 angular 之类的东西来执行 html5 推送状态路由,这将不起作用;页面认为它的 URL 将发生变化。
【解决方案2】:

这适用于 iOS 和 Android(至少对于 2016 平台版本)。

// keep startup url (in case your app is an SPA with html5 url routing)
var initialHref = window.location.href;

function restartApplication() {
  // Show splash screen (useful if your app takes time to load) 
  navigator.splashscreen.show();
  // Reload original app url (ie your index.html file)
  window.location = initialHref;
}

【讨论】:

  • 今天仍然适用于我们的 React cordova 应用程序。谢谢
猜你喜欢
  • 2016-10-22
  • 1970-01-01
  • 2020-06-25
  • 1970-01-01
  • 2013-12-07
  • 2014-11-12
  • 2021-04-02
  • 2022-12-19
  • 1970-01-01
相关资源
最近更新 更多