【uniapp手写组件】多级菜单

多级菜单组件:

<template>
  <view class="menu">
    <view class="menu-item" v-for="item in menuList" :key="item.id" @click="handleClick(item)">
      {{ item.name }}
      <view class="sub-menu" v-if="item.children && item.children.length > 0 && item.open">
        <menu :menu-list="item.children" @handleClick="handleClick"></menu>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'menu',
  props: {
    menuList: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      activeId: ''
    }
  },
  methods: {
    handleClick (item) {
      if (item.children && item.children.length > 0) {
        item.open = !item.open
        this.$set(this.menuList, item.id, item)
      } else {
        this.activeId = item.id
        this.$emit('handleClick', item)
      }
    }
  }
}
</script>

<style>
.menu {
  padding: 10rpx;
}

.menu-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 32rpx;
  padding: 10rpx 0;
  border-bottom: 1px solid #eee;
  cursor: pointer;
}

.sub-menu {
  padding-left: 30rpx;
}
</style>

这个组件是一个递归组件,可以处理多级菜单的情况。它接收一个名为menuList的数组作为参数,这个数组包含了所有的菜单项。当菜单项有子菜单时,用户可以点击它展开或收起子菜单。当用户点击一个菜单项时,如果它没有子菜单,那么它会被激活并触发一个名为handleClick的自定义事件,将被父组件捕获。

### 如何在 UniApp 中实现手写评分组件 为了实现在 UniApp 中的手写评分组件,可以借鉴电子签名的实现方式并加以调整以适应评分需求。通常情况下,这种类型的组件会利用 `<canvas>` 组件来捕捉用户的触摸事件,并允许用户通过手指或触控笔绘制线条。 #### 使用 Canvas 创建基本结构 首先,在页面布局文件中引入 `canvas` 元素: ```html <template> <view class="container"> <!-- 设置 canvas 的 id 和样式 --> <canvas type="2d" :style="{ width: '300px', height: '100px' }" @touchstart="handleTouchStart" @touchmove="handleTouchMove" @touchend="handleTouchEnd"></canvas> <!-- 显示当前得分 --> <text>您的评分为:{{ score }}</text> <!-- 清除按钮 --> <button @click="clearCanvas">清除</button> </view> </template> ``` #### 编写 JavaScript 方法处理交互逻辑 接着编写相应的 Vue.js 脚本部分,这里包含了初始化画布、监听手势动作以及计算最终分数的方法: ```javascript <script> export default { data() { return { ctx: null, startX: 0, startY: 0, moveX: 0, moveY: 0, lineWidth: 5, // 笔迹宽度 strokeColor: '#0000ff', // 颜色 score: 0 // 初始化得分为零 }; }, mounted() { this.ctx = uni.createCanvasContext('myCanvas'); }, methods: { handleTouchStart(e) { const { x, y } = e.touches[0]; this.startX = x; this.startY = y; this.ctx.beginPath(); this.ctx.moveTo(this.startX, this.startY); }, handleTouchMove(e) { const { x, y } = e.touches[0]; this.moveX += (x - this.startX); this.moveY += (y - this.startY); this.ctx.lineTo(x, y); this.ctx.lineWidth = this.lineWidth; this.ctx.strokeStyle = this.strokeColor; this.ctx.stroke(); this.startX = x; this.startY = y; this.updateScore(); // 更新分数 this.$forceUpdate(); // 强制刷新视图 }, updateScore() { // 这里可以根据移动距离或其他标准设定打分机制 let distanceMoved = Math.sqrt(Math.pow(this.moveX, 2) + Math.pow(this.moveY, 2)); // 假设最大可能的距离对应满分5星,则可以通过比例转换成具体分数 const maxDistanceForFullStars = 300; // 设定的最大距离值 this.score = ((distanceMoved / maxDistanceForFullStars) * 5).toFixed(1); if (this.score > 5) { this.score = 5; } }, clearCanvas() { this.ctx.clearRect(0, 0, 300, 100); this.score = 0; this.moveX = 0; this.moveY = 0; } } }; </script> ``` 上述代码展示了如何基于触摸事件捕获用户输入并在屏幕上显示出来的同时动态更新评分[^1]。需要注意的是,这里的评分算法非常简单——它只是根据用户书写的长度给予相应星级评价;实际应用中可根据业务场景设计更加复杂的评估体系。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值