Vue 将 IconFont 封装成组件

SvgIcon.vue

<template>
  <svg class="icon" aria-hidden="true">
    <use :xlink:href="iconName"></use>
  </svg>
</template>


<script >
export default {
    props: ['name'],
    computed:{
      iconName(){
         return `#icon-`+this.name;
      }
    }
}
</script>

<style type="text/css">
  .icon {
    width: 1em;
    height: 1em;
    vertical-align: -0.15em;
    fill: currentColor;
    overflow: hidden;
  }
</style>

在main.js注册成全局组件

  import SvgIcon from '@/components/SvgIcon.vue';
  Vue.component('icon', SvgIcon);

在其它页面调用

<icon name="caozuo-shangchuan-upload"></icon>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个简单的Vue下拉刷新组件封装示例: ```vue <template> <div class="pull-to-refresh" ref="wrapper"> <div class="pull-to-refresh__icon"> <i :class="iconClass"></i> </div> <div class="pull-to-refresh__text">{{text}}</div> <div class="pull-to-refresh__content"> <slot></slot> </div> </div> </template> <script> export default { name: 'PullToRefresh', props: { iconClass: { type: String, default: 'iconfont icon-refresh' }, text: { type: String, default: '下拉刷新' } }, data() { return { startY: 0, moveY: 0, distance: 0, isDragging: false } }, mounted() { this.$refs.wrapper.addEventListener('touchstart', this.handleTouchStart) this.$refs.wrapper.addEventListener('touchmove', this.handleTouchMove) this.$refs.wrapper.addEventListener('touchend', this.handleTouchEnd) }, beforeDestroy() { this.$refs.wrapper.removeEventListener('touchstart', this.handleTouchStart) this.$refs.wrapper.removeEventListener('touchmove', this.handleTouchMove) this.$refs.wrapper.removeEventListener('touchend', this.handleTouchEnd) }, methods: { handleTouchStart(e) { if (this.$refs.wrapper.scrollTop === 0) { this.startY = e.touches[0].clientY this.isDragging = true } }, handleTouchMove(e) { if (!this.isDragging) return this.moveY = e.touches[0].clientY this.distance = this.moveY - this.startY if (this.distance > 0) { e.preventDefault() this.$refs.wrapper.style.transform = `translate3d(0, ${this.distance}px, 0)` } }, handleTouchEnd() { if (!this.isDragging) return this.isDragging = false if (this.distance >= 80) { this.$emit('refresh') } else { this.$refs.wrapper.style.transform = `translate3d(0, 0, 0)` } this.distance = 0 } } } </script> <style scoped> .pull-to-refresh { position: relative; overflow: auto; -webkit-overflow-scrolling: touch; } .pull-to-refresh__icon { font-size: 20px; color: #999; text-align: center; margin-bottom: 10px; } .pull-to-refresh__text { font-size: 14px; color: #999; text-align: center; margin-bottom: 10px; } .pull-to-refresh__content { position: relative; z-index: 1; transform: translate3d(0, 0, 0); transition: all 0.3s ease-in-out; } </style> ``` 这个组件会在用户下拉到一定距离后触发 `refresh` 事件,你可以在使用组件的地方监听该事件并执行对应的操作,例如重新加载数据。 ```vue <template> <pull-to-refresh @refresh="loadData"> <ul> <li v-for="item in items" :key="item.id">{{item.title}}</li> </ul> </pull-to-refresh> </template> <script> import PullToRefresh from './PullToRefresh.vue' export default { components: { PullToRefresh }, data() { return { items: [] } }, methods: { loadData() { // 加载数据的逻辑 this.items = [...] } } } </script> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值