【问题标题】:Ionic2: sidemenu submenu (dropdown)离子:侧面菜单子菜单(下拉)
【发布时间】:2017-12-09 14:05:48
【问题描述】:

我不做 ionic,这不是我的问题,这是我朋友的问题,她害怕在 StackOverflow 上提问,因为她真的不知道如何提问。

她只是想知道如何在 ionic 2 侧菜单启动项目中创建子菜单。

HTML

<ion-menu [content]="content" side="left" id="menu1">
  <ion-content class="outer-content" no-border-top>
    <ion-list lines (click)="openSubCat($event,category)">
      <ion-list-header>
        Shop For
      </ion-list-header>
      <ion-item *ngFor="let item of categoryArray" let idx=index (click)="openSubCat(item.value)">
        <ion-icon [name]="item.icon" item-left></ion-icon>
          {{ item.value }}
        <ion-icon [name]="item.icon2" item-right ></ion-icon>
     </ion-item>
    </ion-list>
 </ion-content>
</ion-menu>
<ion-nav [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>`

app-components.ts

import { Component, ViewChild } from '@angular/core';
import { Nav, Platform } from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';

import { TutorialPage } from '../pages/tutorial/tutorial';


@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  @ViewChild(Nav) nav: Nav;

  rootPage: any = TutorialPage;

  pages: Array<{title: string, component: any}>;

  categoryArray: Array<any> = [{
      value:'Men',
      icon: 'man',
      icon2:'ios-arrow-forward-outline',
      view:'viewToGoTo'
   },{
      value:'Woman',
      icon: 'woman',
      icon2:'ios-arrow-forward-outline',
      view:'viewToGoTo'
   },{
      value:'Kids',
      icon: '',
      icon2:'ios-arrow-forward-outline',
      view:'viewToGoTo'
   }
   ];
  constructor(public platform: Platform) {
    this.initializeApp();
  }

  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();
      Splashscreen.hide();
    });
  }

  openPage(page) {
    // Reset the content nav to have just this page
    // we wouldn't want the back button to show in this scenario
    this.nav.setRoot(page.component);
  }

openSubCat(category){
  console.log(category)

}

}

我给她发了this link,但不能解释太多,因为我不做离子,但似乎要创建一个下拉列表,你只需要创建一个二维数组,对吗?您能否编辑此问题中的代码以包含一个子菜单作为示例供她学习?

【问题讨论】:

    标签: angular typescript ionic-framework ionic2 ionic3


    【解决方案1】:

    [...] 这是我朋友的问题,谁不敢问 StackOverflow,因为她真的不知道怎么问。

    首先告诉你的朋友,她不用害怕在 StackOverflow 上问任何问题,我们都是来学习的,即使问题不好,我们也会帮助她学习如何写一个好问题。

    关于存储库(顺便说一句,感谢分享我的演示,哈哈),这只是在 Ionic 中创建侧边菜单的一种方法。就像您在README.md 文件中看到的一样,首先将side-menu-content 文件夹(.ts、.html 和.scss 文件)的内容复制到您的项目中。

    然后将其添加到NgModuledeclarations 数组中,在app.module.ts 文件中:

    // The path could be different on your end
    import { SideMenuContentComponent } from '../side-menu-content/side-menu-content.component';
    
    @NgModule({
      declarations: [
        MyApp,
        // ...
        SideMenuContentComponent // <- Here!
      ],
      imports: [
        BrowserModule,
        IonicModule.forRoot(MyApp)
      ],
      bootstrap: [IonicApp],
      entryComponents: [
        MyApp,
        // ...
      ],
      providers: [
        StatusBar,
        SplashScreen,
        { provide: ErrorHandler, useClass: IonicErrorHandler }
      ]
    })
    export class AppModule { }
    

    现在我们只需要添加一些代码来初始化侧边菜单,以及处理选择选项时的操作。所以在app.component.ts文件中,添加这段代码:

    @Component({
        templateUrl: 'app.html'
    })
    export class MyApp {
        @ViewChild(Nav) navCtrl: Nav;
    
        // Get the instance to call the public methods
        @ViewChild(SideMenuContentComponent) sideMenu: SideMenuContentComponent;
    
        public rootPage: any = HomePage;
        public options: Array<MenuOptionModel>;
    
        constructor(private platform: Platform,
                    private statusBar: StatusBar,
                    private splashScreen: SplashScreen,
                    private menuCtrl: MenuController) {
            this.initializeApp();
        }
    
        initializeApp() {
            this.platform.ready().then(() => {          
                this.statusBar.styleDefault();
                this.splashScreen.hide();
    
                // Create the options
                this.options = this.getSideMenuOptions();
            });
        }
    
        // Initialize the side menu options
        private getSideMenuOptions(): Array<MenuOptionModel> {
            let menuOptions = new Array<MenuOptionModel>();
    
            // Shop for (header)
            //  - Man
            //  - Woman
            //  - Kids
    
            menuOptions.push({
                iconName: 'ios-arrow-down',
                displayName: `Shop for`,
                component: null,    // This is just the header
                isLogin: false,     // It's not the login
                isLogout: false,    // It's not the logout
                subItems: [
                    {
                        iconName: 'man',
                        displayName: `Men`,
                        component: viewToGoTo,
                        isLogin: false,
                        isLogout: false
                    },
                    {
                        iconName: 'woman',
                        displayName: `Woman`,
                        component: viewToGoTo,
                        isLogin: false,
                        isLogout: false
                    },
                    {
                        iconName: 'happy',
                        displayName: `Kids`,
                        component: viewToGoTo,
                        isLogin: false,
                        isLogout: false
                    }
                ]
            });
    
            return menuOptions;
    
        }
    
        // Redirect the user to the selected page
        public selectOption(option: MenuOptionModel): void {
            this.menuCtrl.close().then(() => {
    
                // Collapse all the options
                this.sideMenu.collapseAllOptions();
    
                // Redirect to the selected page
                this.navCtrl.push(option.component);
            });
        }
    
        public collapseMenuOptions(): void {
            // Collapse all the options
            this.sideMenu.collapseAllOptions();
        }
    }
    

    现在要做的最后一件事是在视图中添加侧边菜单。所以把它放在app.html 文件中:

    <ion-menu persistent="true" [content]="content" (ionClose)="collapseMenuOptions()">
        <ion-header>
            <ion-toolbar color="secondary">
                <ion-title>Menu</ion-title>
            </ion-toolbar>
        </ion-header>
    
        <ion-content>
    
            <!-- Side Menu -->
            <side-menu-content [accordionMode]="true" [options]="options" (selectOption)="selectOption($event)"></side-menu-content>
    
        </ion-content>
    </ion-menu>
    
    <!-- Disable swipe-to-go-back because it's poor UX to combine STGB with side menus -->
    <ion-nav [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>
    

    【讨论】:

    • 嗨@sebaferreras,您能否提供一个使用ionic2+ 和angular3+ 创建的subMenu 演示的工作链接。我只想在包含 ion-list 的现有项目中添加一个子菜单,我真的不想弄乱现有的代码,所以最好提供链接,以便我可以弄清楚布局和工作。
    • 嗨@DhruvSinghal。当然也可以找demo here
    • 非常感谢会看看这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    相关资源
    最近更新 更多