禁用侧面菜单的滑动以打开Ionic 2中的登录页面(或任何页面)的手势

Kar*_*eya 9 menu ionic2 angular

我是Ionic 2和Angular2的新手,我已经使用以下命令下载了一个新的Ionic模板

 Ionic start appname sidemenu --v2 --ts
Run Code Online (Sandbox Code Playgroud)

对于这个特定的解决方案,我添加了一个登录页面来验证用户.验证成功后,用户将导航到使用侧边菜单的菜单页面.

由于解决方案基于sidemenu模板,因此只要用户向左滑动,就会在登录页面上显示侧边菜单.

那么有人可以指导我纠正这个错误,并在浏览视图时停止显示侧边菜单.

我的代码

App.ts文件

import {App, IonicApp, Platform,MenuController} from 'ionic-angular';
import {StatusBar} from 'ionic-native';
import {HelloIonicPage} from './pages/hello-ionic/hello-ionic';
import {ListPage} from './pages/list/list';
import {HomePage} from './pages/home/home';


@App({
  templateUrl: 'build/app.html',
  config: {} // http://ionicframework.com/docs/v2/api/config/Config/
})
class MyApp {
  // make HelloIonicPage the root (or first) page
  rootPage: any = HomePage;
  pages: Array<{title: string, component: any}>;

  constructor(
    private app: IonicApp,
    private platform: Platform,
    private menu: MenuController
  ) {
    this.initializeApp();

    // set our app's pages
    this.pages = [
      { title: 'Hello Ionic', component: HelloIonicPage },
      { title: 'My First List', component: ListPage }
    ];
  }

  initializeApp() {
    this.platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      StatusBar.styleDefault();
    });
  }

  openPage(page) {
    // close the menu when clicking a link from the menu
    this.menu.close();
    // navigate to the new page if it is not the current page
    let nav = this.app.getComponent('nav');
    nav.setRoot(page.component);
  }
}
Run Code Online (Sandbox Code Playgroud)

app.html文件

  <ion-menu side-menu-content drag-content="false"   [content]="content">

  <ion-toolbar>
    <ion-title>Pages</ion-title>
  </ion-toolbar>

  <ion-content>
    <ion-list>
      <button ion-item *ngFor="#p of pages" (click)="openPage(p)">
        {{p.title}}
      </button>
    </ion-list>
  </ion-content>

</ion-menu>

<ion-nav id="nav" [root]="rootPage" #content swipe-back-enabled="false"></ion-nav>
Run Code Online (Sandbox Code Playgroud)

Homepage.ts文件(本例中为登录页面).

   import {Page, Events,Alert,NavController,Loading,Toast,Storage,LocalStorage,SqlStorage} from 'ionic-angular';
import { FORM_DIRECTIVES, FormBuilder, ControlGroup, Validators, AbstractControl } from 'angular2/common';
import {HelloIonicPage} from '../hello-ionic/hello-ionic';
import {NgZone} from 'angular2/core';

@Page({
  templateUrl: 'build/pages/home/home.html'
})
export class HomePage {

 public Uname :string;
 public usrvalid:boolean;
 public usrpwd :boolean;
 public usrpwdlength:boolean;
 public usrvalidlength:boolean;
 public isUnchanged:boolean;
 public usrpwdzero:boolean;
 public usrvaliddigits:boolean;
 rootpage:any;

public Upwd:string;
  constructor(public nav:NavController) {
this.nav=nav;
this.isUnchanged=true;
var mediumRegex = new RegExp("^(((?=.*[a-z])(?=.*[A-Z]))|((?=.*[a-z])(?=.*[0-9]))|((?=.*[A-Z])(?=.*[0-9])))(?=.{6,})");

// rootPage: any = HomePage;

  }
}
Run Code Online (Sandbox Code Playgroud)

Wil*_*ris 16

我认为该drag-content指令用于离子1,对于Ionic 2,您可以做的是在页面类文件中禁用它.

您可以通过MenuController从中导入提供程序来执行此操作ionic-angular,然后调用该.swipeEnable(shouldEnableBoolean, menuId)方法以禁用页面类中的滑动手势(这也在此处记录).

您的登录控制器应该是这样的......

import {Page, MenuController} from 'ionic-angular';

@Page({
    templateUrl: 'build/pages/home/home.html'
})
export class HomePage {
    constructor(public menu: MenuController) {
        this.menu.swipeEnable(false);
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您有多个菜单,每个菜单都有一个ID,那么您可以定位这样的特定菜单......

this.menu.swipeEnable(false, `menuId`);
Run Code Online (Sandbox Code Playgroud)

  • 对于任何无法使swipeEnable(false)工作的人,可能是因为您的菜单尚未初始化.如果您尝试在Hello Ionic模板上执行此操作,它将无法在构造函数中工作,但将在`platform.ready().then`块中 (2认同)