javascript location.reload(true) 已弃用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/55127650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:30:09  来源:igfitidea点击:

location.reload(true) is deprecated

javascriptangulartslint

提问by Dilshan Liyanage

I know that it is not ideal to reload an Angular Single Page Application. However there is a place that I need to reload the full application. TSLint says that reload is deprecated. Is there any alternative for this?

我知道重新加载 Angular 单页应用程序并不理想。但是有一个地方我需要重新加载完整的应用程序。TSLint 表示不推荐使用重新加载。有什么替代方法吗?

回答by Zachary Schroeder

You can use location.reload()without the forceReload flag. This is just deprecated because it is not in the spec: https://www.w3.org/TR/html50/browsers.html#dom-location-reload

您可以在location.reload()没有 forceReload 标志的情况下使用。这只是被弃用,因为它不在规范中:https: //www.w3.org/TR/html50/browsers.html#dom-location-reload

回答by Matt Seymour

If you look at interface definition for Location you will see the following:

如果查看 Location 的接口定义,您将看到以下内容:

/**
  * Reloads the current page.
  */
reload(): void;
/** @deprecated */
reload(forcedReload: boolean): void;

Using location.reload()is the valid syntax. It is only the reload with forcedReload which is deprecated.

Usinglocation.reload()是有效的语法。仅弃用了强制重新加载的重新加载。

In your case changing location.reload(true)to location.reload()will resolve your issue.

在您的情况下,更改location.reload(true)location.reload()将解决您的问题。

回答by Charith Jayasanka

Changing location.reload(true)to location.reload()works. forcedReload is the one which is deprecated.

location.reload(true)更改为location.reload()有效。forceReload 是不推荐使用的。

回答by Martin Schneider

Since the forceReloadparameter is deprecated, as 3 people already mentioned, it is also ignored by some browsers already.

由于该forceReload参数已被弃用,正如 3 个人已经提到的,它也已经被一些浏览器忽略了。

Using only location.reload();is not a solution if you want to perform a force-reload (as done with e.g. Ctrl+ F5) in order to reload all resources from the server and not from the browser cache.

location.reload();如果您想执行强制重新加载(如使用Ctrl+完成F5)以便从服务器而不是浏览器缓存重新加载所有资源,则使用 only不是解决方案。

The solution to this issue is, to execute a POSTrequest to the current location as this always makes the browser to reload everything.

此问题的解决方案是,执行POST对当前位置的请求,因为这总是会使浏览器重新加载所有内容。

location.reload()is only executing a GETrequest.

location.reload()只是执行一个GET请求。

So I came to the workaround to place an empty form on the Angular app with method="POST"and action="https://example.org/"and then submitting that form from code-behind when wanted.

于是,我来到了解决办法将一个空的形式对角应用与method="POST"action="https://example.org/",然后提交从代码隐藏表单想要当。

Template:

模板:

<form [action]="myAppURL" method="POST" #refreshForm></form>

Code-behind / component:

代码隐藏/组件:

import { Component, OnInit, ViewChild } from '@angular/core';

@Component({
  // bla bla bla Mr. Freeman
})
export class FooComponent {
  @ViewChild('refreshForm', { static: true }) refreshForm;

  forceReload() {
    this.refreshForm.nativeElement.submit();
  }
}

I guess this can be also done in a more non-hacky way. I am investigating... (HttpClient, maybe?)

我想这也可以用一种更非hacky的方式来完成。我正在调查......(可能是HttpClient?)