Ionic5:使用sharedModule在所有应用程序页面上设置页脚,并从app.component控制它(打开/关闭



我使用带有菜单的Ionic5创建了一个新应用程序。我正在尝试在多个页面上使用页脚(现在仅在主页上(。首先,我创建了一个SharedModule,并将其导入app.module.ts的imports数组中。我在shared.module.ts的声明和导出数组中添加了页脚组件。此外,我还在每个page.module.ts的importes数组中添加SharedModule,最后在每个page.html中添加<app-footer>。它按预期工作,在所有页面中显示页脚。但现在我需要从我的app.component控制(打开/关闭(这个页脚,以响应特定事件,例如,当互联网不可用时(这部分不是问题(。

页脚组件.ts

import { Component, OnInit } from '@angular/core';
import { FooterService } from 'src/app/footer.service';
@Component({
selector: 'app-footer',
templateUrl: './footer.component.html',
styleUrls: ['./footer.component.scss'],
})
export class FooterComponent implements OnInit {
public FooterEnabled: boolean= false;
constructor() { }
ngOnInit() {
}
}

FooterEnabled变量控制是否显示页脚,并且必须从app.component 中进行修改

footer.component.html

<div class="footer-conn" *ngIf="FooterEnabled">
Alert!
</div>

sharedfooter.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FooterComponent } from '../components/footer/footer.component';
@NgModule({
declarations: [FooterComponent],
imports: [
CommonModule
],
exports: [
FooterComponent, CommonModule
]
})
export class SharedFooterModule { }

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';
import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { Network } from '@ionic-native/network/ngx';
import { SharedFooterModule } from './shared/sharedfooter.module';
@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [
BrowserModule,
IonicModule.forRoot(),
AppRoutingModule,
SharedFooterModule
],
providers: [
StatusBar,
SplashScreen,
Network,

{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule {}

app.component.ts

import { Component, OnInit } from '@angular/core';
import { Platform } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { Network } from '@ionic-native/network/ngx';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.scss']
})
export class AppComponent implements OnInit {
public selectedIndex = 0;
public appPages = [
{
title: 'Página 1',
url: 'home',
icon: 'mail'
}, 
{
title: 'Página 2',
url: 'pagina2',
icon: 'paper-plane'
},
{
title: 'Página 3',
url: 'pagina3',
icon: 'heart'
}
];  
constructor(
private platform: Platform,
private splashScreen: SplashScreen,
private statusBar: StatusBar,
private network: Network
) {

this.initializeApp();   

}// Fin constructor
no_internet() {
alert("No internet!")
// In this point make FooterEnabled = true (from the footer component)
}
si_internet() {
alert("Whith internet!") 
//In this point make FooterEnabled = false (from the footer component)
}
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();

});
}
ngOnInit() {
let disconnectSubscription = this.network.onDisconnect().subscribe(() => {
setTimeout(() => {
if (this.network.type !== 'none') {
this.si_internet();
}
else {
this.no_internet();
}
}, 1000);

});
// watch network for a connection
let connectSubscription = this.network.onConnect().subscribe(() => {
setTimeout(() => {
if (this.network.type !== 'none') {
this.si_internet();
}
else {
this.no_internet(); 
}
}, 3000);
});
const path = window.location.pathname.split('/')[1];
if (path !== undefined) {
this.selectedIndex = this.appPages.findIndex(page => page.title.toLowerCase() === path.toLowerCase());
}
}
}

home.module.ts(作为示例(

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { IonicModule } from '@ionic/angular';
import { HomePageRoutingModule } from './home-routing.module';
import { HomePage } from './home.page';
import { SharedFooterModule } from '../shared/sharedfooter.module';
@NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
HomePageRoutingModule,
SharedFooterModule
],
declarations: [HomePage]
})
export class HomePageModule {}

我尝试过在footer.component和app.component.ts中导入一个实现可观察性的服务,但它不起作用。我将感谢你的贡献!!

也许你可以像下面这样做,将输入添加到你的页脚组件:

页脚组件.ts

import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-footer',
templateUrl: './footer.component.html',
styleUrls: ['./footer.component.scss'],
})
export class FooterComponent implements OnInit {
@Input() FooterEnabled : string;
//public FooterEnabled: boolean= false;
constructor() { }
ngOnInit() {
}
}

页脚组件.ts

<div class="footer-conn" *ngIf="FooterEnabled == 'on'">
Alert!
</div>

在您的应用程序组件上添加新的变量状态:

no_internet() {
alert("No internet!")
this.status = 'on'
}
si_internet() {
alert("Whith internet!") 
this.status = 'off'
}

把它放在app.component.html上作为:

<app-footer FooterEnabled="status" ></app-footer>

相关内容

  • 没有找到相关文章

最新更新