3 回答
![?](http://img1.sycdn.imooc.com/54584c910001b8d902200220-100-100.jpg)
TA贡献1785条经验 获得超4个赞
每当您在新选项卡中打开应用程序时,它都会创建一个新的应用程序实例,这意味着两个应用程序分别运行。他们之间没有任何关系。为了控制在两个不同选项卡中运行的应用程序状态,您需要具有可以存储在浏览器本地存储中的公共控制变量。
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');
}
}
第一次打开应用程序时,您将显示导航栏,您应该在应用程序已打开的本地存储中设置状态。在构造函数中,您应该检查应用程序是否已打开。如果打开,则将 showNavbar 设置为 false。这个 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>
您可能希望在应用程序关闭时清除存储空间。
![?](http://img1.sycdn.imooc.com/54586425000103f602200220-100-100.jpg)
TA贡献1821条经验 获得超4个赞
每当您在新选项卡中打开应用程序时,它都会创建一个新的应用程序实例,这意味着两个应用程序分别运行。他们之间没有任何关系。为了控制在两个不同选项卡中运行的应用程序状态,您需要具有可以存储在浏览器本地存储中的公共控制变量。
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');
}
}
第一次打开应用程序时,您将显示导航栏,您应该在应用程序已打开的本地存储中设置状态。在构造函数中,您应该检查应用程序是否已打开。如果打开,则将 showNavbar 设置为 false。这个 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>
您可能希望在应用程序关闭时清除存储空间。
添加回答
举报