ionic3的侧栏菜单案例,这个案例中,主要做了三个事情:1、影藏了默认的返回按钮。2、隐藏了底部导航栏,3、实现侧滑菜单。其实这几样东西看起来挺简单的,但是我也是费了不少时间啊,破玩意刚玩,整得不太明白啊。
在点击时间里面,由于item对象是object的,而不是string类型的数据,所以我加上了”“,转化为string类型的数据
//点击了哪一条消息 clickItem(item){ console.log(item); this.showInfo(item+""); } //显示toast消息 showInfo(name){ let toast = this.toastCtrl.create({ message: '点击了'+name, //提示消息 duration: 3000,//3秒后自动消失 position: 'bottom',//位置top,bottom showCloseButton:true, //是否显示关闭按钮 closeButtonText:"关闭" //关闭按钮字段 }); //关闭后执行的操作 toast.onDidDismiss(() => { console.log('toast被关闭之后执行'); }); //显示toast toast.present();//符合触发条件后立即执行显示。 }在界面没有加载完成的时候,设定tabbar在加载的时候,影藏,然后在退出的时候显示,这个功能可以解决有些特定的界面,需要影藏导航栏的情况。
//进入界面资源还没有加载完成的情况 ionViewDidLoad() { console.log('ionViewDidLoad FriendsPage'); // document.getElementById(); var tabs = document.getElementsByClassName('tabbar').item(0); tabs['style'].display = 'none'; } //离开页面的时候,设置显示下面的tabbar ionViewWillLeave(){ var tabs = document.getElementsByClassName('tabbar').item(0); tabs['style'].display = 'flex'; }其实这个ionic3玩的其实是一个html的游戏,直接在浏览器中查看 tabbar在浏览器所对应的html的代码,然后,在代码中,像js一样,获取到节点对象信息,然后直接隐藏掉。
界面代码中,主要注意的是如何隐藏掉返回菜单。
<ion-header> <!--去掉这个返回菜单--> <ion-navbar hideBackButton="true" hide-nav-bar="true"> <ion-title> 侧滑菜单 </ion-title> </ion-navbar> </ion-header> <ion-content> <h1>侧滑菜单</h1> <ion-menu [content]="mycontent"> <ion-content> <!--菜单--> <ion-list> <ion-item *ngFor="let item of menus" (click)="clickItem(item)" > {{item}} </ion-item> </ion-list> </ion-content> </ion-menu> <ion-nav #mycontent [root]="rootPage"></ion-nav> <!--返回--> <button ion-button (click)="back()">反回</button> <!--打开和关闭侧栏--> <button ion-button (click)="toggoleMenu()">打开/关闭侧栏</button> </ion-content>https://ionicframework.com/docs/api/components/app/MenuController/