【问题标题】:New url is getting appended with localhost url新 url 被附加 localhost url
【发布时间】:2017-10-17 00:55:46
【问题描述】:

我试图在新标签页中打开的用户动态获取的 URL,它在新标签页中打开,但 localhost url 与动态 url 一起附加。

      @Injectable()
    export class WindowRef {
        constructor() {}
        getNativeWindow() {
            return window;
         }
    }

export class UrlTab implements OnInit{

          constructor(private winRef: WindowRef) {
           this.nativeWindow = winRef.getNativeWindow();
       }

    //method getting called on click of <a></a> from html
          assignActity(type: string): void {
           var newWindow = this.nativeWindow.open("www.gmail.com");
    }
}

【问题讨论】:

    标签: angular


    【解决方案1】:

    您在调用.open() 方法调用时需要包含协议(“http://”、“https://”等)。

    示例:

    var newWindow = this.nativeWindow.open("http://www.gmail.com");

    或者,更好的是,您可以通过在 URL 前仅添加“//”来使其“无协议”。

    示例:

    var newWindow = this.nativeWindow.open("//www.gmail.com");

    当省略协议时,浏览器假定您指的是相对 URL。

    【讨论】:

    • 但有一个问题是当前窗口在打开新窗口时会重新加载。
    猜你喜欢
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 2019-06-25
    • 2021-12-27
    • 2015-03-23
    • 2011-02-21
    相关资源
    最近更新 更多