当前位置:首页 > uni-app

uniapp图标事件

2026-03-05 00:37:42uni-app

添加图标事件的基本方法

在UniApp中为图标添加事件通常通过@click@tap绑定方法实现。图标可以是<image>标签、<uni-icons>组件或第三方图标库(如Font Awesome)。

<template>
  <view>
    <!-- 使用uni-icons组件 -->
    <uni-icons type="contact" size="30" @click="handleIconClick"></uni-icons>

    <!-- 使用image标签 -->
    <image src="/static/icon.png" mode="widthFix" @tap="handleImageClick"></image>
  </view>
</template>

<script>
export default {
  methods: {
    handleIconClick() {
      uni.showToast({ title: '图标被点击' });
    },
    handleImageClick() {
      uni.navigateTo({ url: '/pages/detail/detail' });
    }
  }
}
</script>

使用第三方图标库

通过uni-icons以外的图标库(如Font Awesome)时,需引入对应CSS文件,并通过<text><view>绑定事件:

uniapp图标事件

<template>
  <view>
    <!-- Font Awesome示例 -->
    <text class="fas fa-home" @click="navigateHome"></text>
  </view>
</template>

<script>
export default {
  methods: {
    navigateHome() {
      uni.switchTab({ url: '/pages/home/home' });
    }
  }
}
</script>

<style>
@import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css');
</style>

动态控制图标状态

结合数据绑定实现图标状态切换(如收藏按钮):

<template>
  <view>
    <uni-icons 
      :type="isFavorited ? 'star-filled' : 'star'" 
      @click="toggleFavorite">
    </uni-icons>
  </view>
</template>

<script>
export default {
  data() {
    return {
      isFavorited: false
    }
  },
  methods: {
    toggleFavorite() {
      this.isFavorited = !this.isFavorited;
      uni.showToast({
        title: this.isFavorited ? '已收藏' : '取消收藏'
      });
    }
  }
}
</script>

图标事件传参

通过data-*属性传递参数给事件处理函数:

uniapp图标事件

<template>
  <view>
    <uni-icons 
      v-for="item in icons" 
      :key="item.id"
      :type="item.type" 
      :data-id="item.id"
      @click="handleIconWithParam">
    </uni-icons>
  </view>
</template>

<script>
export default {
  data() {
    return {
      icons: [
        { id: 1, type: 'home' },
        { id: 2, type: 'settings' }
      ]
    }
  },
  methods: {
    handleIconWithParam(e) {
      const id = e.currentTarget.dataset.id;
      console.log('点击的图标ID:', id);
    }
  }
}
</script>

阻止事件冒泡

使用@click.stop修饰符阻止事件冒泡:

<template>
  <view @click="handleParentClick">
    <uni-icons type="close" @click.stop="handleClose"></uni-icons>
  </view>
</template>

<script>
export default {
  methods: {
    handleParentClick() {
      console.log('父元素点击');
    },
    handleClose() {
      console.log('仅触发图标点击');
    }
  }
}
</script>

图标长按事件

通过@longpress实现长按触发:

<template>
  <view>
    <uni-icons 
      type="trash" 
      @longpress="showDeleteConfirm">
    </uni-icons>
  </view>
</template>

<script>
export default {
  methods: {
    showDeleteConfirm() {
      uni.showModal({
        title: '确认删除',
        content: '长按触发删除操作'
      });
    }
  }
}
</script>

标签: 图标事件
分享给朋友:

相关文章

vue事件实现

vue事件实现

Vue 事件实现方法 模板中直接绑定事件 在 Vue 模板中通过 v-on 或 @ 语法直接绑定事件。例如点击事件: <button @click="handleClick">点击触发事…

vue实现change事件

vue实现change事件

Vue 实现 change 事件的方法 在 Vue 中,可以通过多种方式监听和处理 change 事件,以下是常见的实现方法: 使用 v-on 或 @ 语法监听 change 事件 <t…

vue事件循环实现

vue事件循环实现

Vue 的事件循环实现 Vue 的事件循环机制主要依赖于 JavaScript 的原生事件循环(Event Loop),并结合 Vue 自身的响应式系统和异步更新队列机制。以下是 Vue 事件循环的核…

vue实现页面单击事件

vue实现页面单击事件

Vue 实现页面单击事件的方法 在 Vue 中实现单击事件可以通过 v-on 指令或 @ 简写来完成。以下是几种常见的实现方式: 使用 v-on 指令绑定单击事件 通过 v-on:click 可以…

vue点击事件实现跳转

vue点击事件实现跳转

vue点击事件实现跳转 在Vue中实现点击事件跳转可以通过多种方式完成,具体取决于跳转的目标和需求。以下是几种常见的方法: 使用router-link组件 router-link是Vue Rout…

vue实现关闭页面事件

vue实现关闭页面事件

Vue 实现页面关闭事件 在 Vue 中监听页面关闭事件,可以通过 beforeunload 或 unload 事件实现。以下是几种常见方法: 方法一:使用 window.addEventListe…