简体   繁体   English

离子项目可点击属性不起作用(离子 4)

[英]ion-item clickable properties not working (Ionic 4)

BACKGROUND: I have a dashboard for my app with 3 ion-items, I want them to be clickable so as someone clicks/taps on the item it will navigate to a different page.背景:我的应用程序有一个仪表板,其中包含 3 个离子项目,我希望它们可以点击,以便当有人点击/点击该项目时,它会导航到不同的页面。

Here is a screenshot to give a better understanding: https://imgur.com/TCv3pMc这是一个截图,以便更好地理解: https : //imgur.com/TCv3pMc

Here is a link to my github repo with all the files: https://github.com/jamesslater/boutiquesolicitors这是我的 github 存储库的链接,其中包含所有文件: https : //github.com/jamesslater/boutiquesolicitors

PROBLEM: To do this I've been trying to call a method which routes to the correct page.问题:为此,我一直在尝试调用一个路由到正确页面的方法。 However, while testing with a console.log I have noticed both (click) and href do not work at all .但是,在使用 console.log 进行测试时,我注意到 (click) 和 href根本不起作用

Can someone please explain how I can get this working?有人可以解释一下我如何让这个工作吗? Any help is greatly appreciated!任何帮助是极大的赞赏!

RELEVANT CODE:相关代码:

dashboard.page.html仪表板.page.html

<div class="nav-wrapper" style="background: white;">
<ion-header class="navbar">

    <ion-buttons slot="end">
      <ion-button (click)="logout()">
        <ion-icon slot="icon-only" color="white" name="log-out"></ion-icon>
      </ion-button>
    </ion-buttons>
    <ion-img src="assets/logo.png"></ion-img>


</ion-header>
 </div>
<ion-content padding text-center>
  <div class="profile">
  <ion-avatar>
<img src="assets/user.png">
</ion-avatar>
<h1>Hello Richard,</h1>
<ion-label>How can we help you today?</ion-label>
  </div>


    <ion-item (click)="search()">
      <ion-icon name="search" slot="start"></ion-icon>
      <ion-label>Search for a service</ion-label>    
    </ion-item>
    <ion-item>
    <ion-icon name="paper" slot="start"></ion-icon>
    <ion-label>View our latest news</ion-label>
  </ion-item>
  <ion-item>
  <ion-icon name="card" slot="start"></ion-icon>
  <ion-label>Pay a bill</ion-label>
</ion-item>


  <ion-tabs>
    <ion-tab-bar slot="bottom">
      <ion-tab-button tab="about">
       <ion-icon name="information-circle-outline"></ion-icon>
        <ion-label>About Us</ion-label>
      </ion-tab-button>
      <ion-tab-button href="members/dashboard">

        <ion-icon color="blue" name="home"></ion-icon>
        <ion-label>Dashboard</ion-label>
      </ion-tab-button>
      <ion-tab-button tab="contact">
        <ion-icon name="contacts"></ion-icon>
        <ion-label>Contact Us</ion-label>
      </ion-tab-button>
    </ion-tab-bar>
  </ion-tabs>
</ion-content>

dashboard.page.ts仪表板.page.ts

import { AuthenticationService } from './../../services/authentication.service';
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.page.html',
  styleUrls: ['./dashboard.page.scss'],
})
export class DashboardPage implements OnInit {
error = this.error; 
  constructor(private authService: AuthenticationService, private router: Router) { }

  ngOnInit() {
  }

  logout() {
    this.router.navigate(['login']);
    // this.authService.logout();
  }

  search() {
    console.log('clicked');
    this.router.navigate(['members/services']);
  }
}

As you have stated, ion items and ion tabs do not work in unison for some unknown reason.正如您所说,由于某些未知原因,离子项目和离子选项卡不能同时工作。 Wouldn't be known to the usual developer as this is a strange use case (which nobody else has picked up on above).通常的开发人员不会知道,因为这是一个奇怪的用例(上面没有其他人注意到)。 Good thing I saw this question, otherwise you'd NEVER get this sorted mate.还好我看到了这个问题,否则你永远不会得到这个排序的伙伴。 Surprised you even managed to consider this as one of the factors, well done for that one, really helps with the resolution for this:令您惊讶的是,您甚至设法将其视为因素之一,做得很好,确实有助于解决此问题:

Best way to go about doing this: Use a transparent ion-button inside of an ion item.执行此操作的最佳方法:在离子项目内使用透明的离子按钮。

HTML/IONIC Code For Button:按钮的 HTML/IONIC 代码:

    <div class="items">
   <ion-item detail> 
     <ion-button fill="clear" class="itemBtns" (click)="search()">Search for a service</ion-button>
     <ion-icon name="search" slot="start"></ion-icon> 
     <!-- <ion-label>Search for a service</ion-label>  -->
    </ion-item>

This creates a 'click zone' which is the way of bypassing the issue of not being able to use ion items and ion tabs at the same time.这会创建一个“点击区”,这是绕过无法同时使用离子项目和离子选项卡的问题的方法。

CSS for button:按钮的 CSS:

.itemBtns {
color: black;
font-size: 100%;
padding-right: 100%;  }

With a combination of the 'fill' property on the button in the HTML code, this makes the button invisible and should act as a hyperlink that spans the whole section of the ion item.结合 HTML 代码中按钮上的 'fill' 属性,这会使按钮不可见,并应充当跨越整个离子项目部分的超链接。

Please mark this as the correct answer if I have helped you.如果我对您有帮助,请将其标记为正确答案。 If not, let me know and I will offer further assistance.如果没有,请告诉我,我会提供进一步的帮助。 :) :)

With ionic 5 you can add property button to ion-item and click event will work:使用 ionic 5,您可以将属性按钮添加到 ion-item 并且单击事件将起作用:

<ion-item button (click)="search()">
      <ion-icon name="search" slot="start"></ion-icon>
      <ion-label>Search for a service</ion-label>    
  </ion-item>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM