简体   繁体   English

如何删除打开的选项卡上的导航栏

[英]How to remove nav bar on the opened tab

I want to open the current window in a new tab but without the navigation bar 我想在新选项卡中打开当前窗口但没有导航栏

I have used window.open to open in a new tab and document.getElementById to remove the navigation bar. 我使用window.open在新选项卡中打开,使用document.getElementById删除导航栏。

https://stackblitz.com/edit/angular-6-routing-pu4kjz https://stackblitz.com/edit/angular-6-routing-pu4kjz

 openNewTab() {
    window.open('' + this.href, '_blank').focus();
    document.getElementById('navigation').outerHTML = '';
  }
<button style="background:red;" 
   (click)="openNewTab()" 
   routerLink="/{{ href }}">
     Open-In-New-Tab >
</button>

Actual result: When the button is clicked, the navigation bar is removed from the current window but on the new tab the navigation bar remains. 实际结果:单击按钮时,导航栏将从当前窗口中删除,但在新选项卡上将保留导航栏。

Expected result: When the button is clicked - I would like the current window to stay the same (with navbar). 预期结果:单击按钮时 - 我希望当前窗口保持不变(使用导航栏)。 The new tab should open without the navbar. 新选项卡应在没有导航栏的情况下打开。

A simple solution would be to pass some query param like: 一个简单的解决方案是传递一些查询参数,如:

window.open('' + this.href + '?show=false', '_blank').focus();

then in your component ts disable navigation based on this param 然后在你的组件ts禁用基于此参数的导航

Then your component should be something like: 然后你的组件应该是这样的:

import { Component } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';


@Component({
  selector: 'about',
  templateUrl: './about.component.html'
})

export class About{
  title: string = "About Page.";
    public href: String = '';

    constructor(private router: Router, private route:ActivatedRoute)
{ }
  openNewTab() {
    window.open('' + this.href + '?show=false', '_blank').focus();
    document.getElementById('navigation').outerHTML = '';
  }

  ngOnInit() {
    this.href = decodeURI(this.router.url.replace(/\/.*\//g, ''));
    this.route.queryParams.subscribe(params => {
      if (params.show === 'false') {
        document.getElementById('navigation').outerHTML = '';
      }
    });
  }
}

Whenever you open the app in a new tab, it creates a new instance of app, which means that two apps running separately. 每当您在新选项卡中打开应用程序时,它都会创建一个新的应用实例,这意味着两个应用程序单独运行。 There is no relation in between them. 它们之间没有关系。 In order to control the app state running in two different tabs, you need to have common control variables which can be stored in browsers local storage. 为了控制在两个不同选项卡中运行的应用程序状态,您需要具有可以存储在浏览器本地存储中的公共控制变量。

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular 6 Router with Guard';

  showNavBar : boolean = true

  constructor() {

    if(window.localStorage.getItem('alreadyOpened') === 'true') 
      this.showNavBar = false
    else
      window.localStorage.setItem('alreadyOpened', 'true');
  }
}

For the first time when you open the app you will display navbar and you should set a status in the local storage that app was already opened. 当您第一次打开应用程序时,您将显示导航栏,您应该在本地存储中设置已打开应用程序的状态。 In the constructor you should check if app is already opened. 在构造函数中,您应该检查应用程序是否已经打开。 If opened then set the showNavbar to false. 如果打开,则将showNavbar设置为false。 This showNavbar variable will control the display of navbar in the html using NgIf. 这个showNavbar变量将使用NgIf控制html中导航栏的显示。

<div class="container">
<nav style="background-color: yellow;" id="navigation" class="navbar navbar-default" *ngIf="showNavBar">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="#"><hello name="{{ name }}"></hello></a>
    </div>
    <ul class="nav navbar-nav pull-right">
      <li routerLinkActive="active"><a [routerLink]="['/about']">About</a></li>
      <li routerLinkActive="active"><a [routerLink]="['/service']">Service</a></li>
      <li routerLinkActive="active"><a [routerLink]="['/dashboard']">Dashboard</a></li>
          </ul>
  </div>
</nav>
  <myComp></myComp>
  <router-outlet></router-outlet>
</div>

And you might want to clear the storage when the app is closed. 您可能希望在应用关闭时清除存储空间。

When you use document.getElementById('navigation') , you are selecting the navigation bar on the current tab, not on the new tab. 使用document.getElementById('navigation') ,您选择的是当前选项卡上的导航栏,而不是新选项卡上的导航栏。

When you use window.open , you get a reference to the newly opened window. 当您使用window.open ,您将获得对新打开的窗口的引用。 Then you can execute code when the window is loaded. 然后,您可以在加载窗口时执行代码。

openNewTab() {
  const newTab = window.open('' + this.href, '_blank');
  newTab.window.onload = function () {      
      // Add logic for new tab here
  }
}

However, since this is an angular application, app initialization might not be complete when the window is loaded. 但是,由于这是一个角度应用程序,因此在加载窗口时,应用程序初始化可能不完整。 So, you might not be able to use newTab.document.getElementById('navigation').outerHTML = ''; 所以,你可能无法使用newTab.document.getElementById('navigation').outerHTML = ''; . An alternative would be to set a class to the body and then add logic in your angular app to not render navigation if body has the class. 另一种方法是将一个类设置为正文,然后在角度应用程序中添加逻辑,以便在body具有类时不呈现导航。

openNewTab() {
  const newTab = window.open('' + this.href, '_blank');
  newTab.window.onload = function () {      
    newTab.document.body.classList.add('removeHeader');
  }
}

You can now modify your app.component to not render the navigation bar if body has the class removeHeader . 现在,您可以修改app.component以便在body具有类removeHeader不渲染导航栏。 Add a flag to your app.component to check for this condition. app.component添加一个标志以检查此情况。

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  name = 'Angular 6 Router with Guard';
  displayNavigation = true;

  ngOnInit() {
    this.displayNavigation = !document.body.classList.contains('removeHeader');
  }
}

Now, use displayNavigation as *ngIf on your nav in app.component 现在,在app.component中的导航上使用displayNavigation作为*ngIf ngIf

<nav style="background-color: yellow;" id="navigation" *ngIf="displayNavigation" class="navbar navbar-default">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM