【问题标题】:(click) event not working on majority of elements on page - Ionic 4(点击)事件不适用于页面上的大多数元素 - Ionic 4
【发布时间】:2019-12-24 16:37:35
【问题描述】:

背景:

一段时间以来,所有点击事件都对我的 Ionic 应用程序起作用,然后突然停止。我有两个曾经有工作点击事件的浮动操作按钮;一个会触发一个 JS 警报弹出窗口,另一个会打开一个模式。

这些点击事件不再起作用,现在唯一具有有效点击事件的元素是我的 ion-tab-buttons。

我尝试过/测试过的内容:

  • 我已经尝试从 ion-items 和 ion-lists 中解开这些元素,但仍然没有任何变化。

  • 一个简单的离子按钮作为替代品,仍然没有变化。

  • 将 ion-button 移到 card 元素之外,就在开始 ion-content 标签的下方,仍然没有变化。

  • 将离子按钮移到离子内容之外;就在选项卡上方,仍然没有变化。

非常感谢任何帮助。

contact.page.html

<ion-header class="navbar">
  <div class="icons">
    <ion-buttons slot="start">
      <ion-menu-button color="tertiary"></ion-menu-button>
    </ion-buttons>
    <img src="assets/logo.png" />
  </div>
</ion-header>


<ion-content text-center>

  <ion-card>
    <ion-card-header>
      <ion-card-title>Contact Us</ion-card-title>
      <ion-card-subtitle>BOUTIQUE SOLICITORS</ion-card-subtitle>
    </ion-card-header>
    <br>
    <ion-card-content>

      <ion-label><b>Head Office: Wembley</b>
        <br>
        Boutique Immigration Solicitors, 10th Floor Tower, 1 Olympic Way, Wembley, Middlesex HA9 0NP
        DX: 51165 Wembley Park
        We are located just 5 minutes from Wembley Park Station. There is a car park behind our office.
      </ion-label>

      <br>
      <br>

      <ion-list text-left>
        <ion-item>
          <ion-label>Call us on 0800 3881 0211</ion-label>
          <ion-fab-button color="secondary" slot="end" size="small" (click)="callAlert()">
            <ion-icon name="call"></ion-icon>
          </ion-fab-button>
        </ion-item>
        <ion-item>
          <ion-label>Email at admin@boutiquesolicitors.co.uk</ion-label>
          <ion-fab-button color="secondary" slot="end" size="small" (click)="modalEmail()">
            <ion-icon name="mail"></ion-icon>
          </ion-fab-button>
        </ion-item>
      </ion-list>
    </ion-card-content>
  </ion-card>
</ion-content>

<ion-tabs>
  <ion-tab-bar slot="bottom">
    <ion-tab-button (click)="about()">
      <ion-icon name="information-circle-outline"></ion-icon>
      <ion-label>About Us</ion-label>
    </ion-tab-button>
    <ion-tab-button (click)="dashboard()">
      <ion-icon color="blue" name="home"></ion-icon>
      <ion-label>Dashboard</ion-label>
    </ion-tab-button>
    <ion-tab-button class="activeTab">
      <ion-icon name="contacts"></ion-icon>
      <ion-label>Contact Us</ion-label>
    </ion-tab-button>
  </ion-tab-bar>
</ion-tabs>

contact.page.ts

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router'
import { CallNumber } from '@ionic-native/call-number/ngx';
import { ModalController, AlertController } from '@ionic/angular';
import { EmailPage } from '../email/email.page';

@Component({
  selector: 'app-contact',
  templateUrl: './contact.page.html',
  styleUrls: ['./contact.page.scss'],
})
export class ContactPage implements OnInit {

  constructor(private router: Router, private callNumber: CallNumber, private alertController: AlertController, private modalController: ModalController) { }

  ngOnInit() {
  }

  about() {
    console.log('clicked about')
    this.router.navigate(['members/menu/about'])
  }

  dashboard() {
    console.log('clicked dashboard')

    this.router.navigate(['members/menu/dashboard'])
  }

  async callAlert() {
    console.log('method executed')
    const callAlert = await this.alertController.create({
      message: "Are you sure you want to call Boutique Solicitors?",
      buttons: [{
        text: "Cancel",
        role: "cancel"

      },
      {
        text: "Call",
        handler: () => {this.callBS()}
      }
    ]
    });
    callAlert.present()

  }

  async callBS() {

    this.callNumber.callNumber("07847948252", false)
  .then(res => console.log('Launched dialer!', res))
  .catch(err => console.log('Error launching dialer', err))
  }

  async modalEmail() {
    const modal = await this.modalController.create ({
      component: EmailPage
    });
    modal.present();
  }

}

【问题讨论】:

    标签: javascript angular typescript ionic-framework


    【解决方案1】:

    上面@mhartington 的答案是正确的答案。对于 Angular,选项卡应该是在 tabs-routing.module.ts 中加载其他页面作为子模块的页面。

    【讨论】:

      【解决方案2】:

      此处提供的答案不正确。关于选项卡如何从 OP 工作存在误解。选项卡是整页 UI 组件,而不是嵌入到其他页面/组件中。这意味着它们应该是页面中的唯一内容。

      <ion-tabs>
      
        <ion-tab-bar slot="bottom">
          <ion-tab-button tab="tab1">
            <ion-icon name="triangle"></ion-icon>
            <ion-label>Tab 1</ion-label>
          </ion-tab-button>
      
          <ion-tab-button tab="tab2">
            <ion-icon name="ellipse"></ion-icon>
            <ion-label>Tab 2</ion-label>
          </ion-tab-button>
      
          <ion-tab-button tab="tab3">
            <ion-icon name="square"></ion-icon>
            <ion-label>Tab 3</ion-label>
          </ion-tab-button>
        </ion-tab-bar>
      
      </ion-tabs>
      

      这样,选项卡负责渲染嵌套内容,事件按预期工作。

      【讨论】:

      • 你能编辑你的代码并向我们展示如何在标签中包含我们的离子内容吗?
      • 你没有。选项卡将通过路由器加载组件。所以对于 angular 或 vue 它需要它的路由器配置,react 将有一个路由器出口组件
      • 很高兴我没有继续上面的“Hack”,谢谢
      【解决方案3】:

      好吧,我最终自己弄清楚了。问题是,当 ion-tabs 放置在底部的“ion-content”之外时,如果您在 Chrome Dev Tools 上检查元素,您会看到由于某种原因它们的“点击区域”占据了整个页面,并且在所有其他元素之前被索引。尝试使用“z-index”css 属性无法解决问题。

      解决方法是将 ion-tabs 包装在 ion-toolbar 中,在关闭的“ion-content”标签下方。这会将点击区域修复到受限的离子工具栏高度,从而解决问题。现在将触发所有其他页面元素(单击)事件。这也解决了选项卡何时可以阻止页面滚动的问题。

      在 Android 和 iOS 上测试。

      喵喵。

      【讨论】:

      • 我也遇到过这个问题,非常感谢!我登录只是为了支持你的答案..
      • 这个解释使我免于遇到的几个问题。遇到这个问题真的很幸运! ?非常感谢
      • 修复方法是将 ion-tabs 包裹在 ion-toolbar 中,在关闭的 'ion-content' 标签下方。谢谢......像魅力一样工作......??
      • 伙计,如果你曾经需要一个肾脏,我有两个……!这几天一直在处理这个问题,让我发疯了,你是一个救生员,谢谢
      • 我为 Ionic 工作并为框架提供帮助...这不是解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多