当前位置: 首页 > news >正文

用 Deepseek 写的uniapp油耗计算器

下面是一个基于 Uniapp 的油耗计算器实现,包含 Vue 组件和页面代码。

1. 创建页面文件

在 pages 目录下创建 fuel-calculator 页面:

<!-- pages/fuel-calculator/fuel-calculator.vue -->
<template><view class="container"><view class="calculator"><view class="header"><text class="title">油耗计算器</text></view><view class="input-group"><text class="label">当前油价 (元/升)</text><input type="number" v-model="price" placeholder="例如:7.85" class="input"@input="validateInput('price')"/></view><view class="input-group"><text class="label">加油金额 (元)</text><input type="number" v-model="money" placeholder="例如:300" class="input"@input="validateInput('money')"/></view><view class="input-group"><text class="label">行驶里程 (公里)</text><input type="number" v-model="distance" placeholder="例如:450" class="input"@input="validateInput('distance')"/></view><button class="calculate-btn" @click="calculate">计算油耗</button><view class="result" v-if="showResult"><view class="result-header"><text class="result-title">计算结果</text></view><view class="result-item"><text>加油量:</text><text class="result-value">{{ fuel.toFixed(2) }} 升</text></view><view class="result-item"><text>百公里油耗:</text><text class="result-value">{{ consumption.toFixed(2) }} 升/百公里</text></view><view class="result-item"><text>每公里油费:</text><text class="result-value">{{ costPerKm.toFixed(2) }} 元</text></view></view></view></view>
</template><script>
export default {data() {return {price: '',money: '',distance: '',fuel: 0,consumption: 0,costPerKm: 0,showResult: false}},methods: {validateInput(field) {// 确保输入是正数if (this[field] < 0) {this[field] = ''}},calculate() {// 验证输入if (!this.price || !this.money || !this.distance) {uni.showToast({title: '请填写完整信息',icon: 'none'})return}if (this.price <= 0 || this.money <= 0 || this.distance <= 0) {uni.showToast({title: '请输入大于0的数值',icon: 'none'})return}// 计算this.fuel = this.money / this.pricethis.consumption = (this.fuel / this.distance) * 100this.costPerKm = this.money / this.distancethis.showResult = true// 隐藏键盘uni.hideKeyboard()}}
}
</script><style>
.container {padding: 20px;background-color: #f5f5f5;min-height: 100vh;
}.calculator {background-color: #ffffff;border-radius: 10px;padding: 20px;box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}.header {margin-bottom: 25px;
}.title {font-size: 22px;font-weight: bold;color: #333;text-align: center;display: block;
}.input-group {margin-bottom: 20px;
}.label {font-size: 16px;color: #555;font-weight: bold;display: block;margin-bottom: 8px;
}.input {border: 1px solid #ddd;border-radius: 5px;padding: 12px;font-size: 16px;width: 100%;box-sizing: border-box;
}.calculate-btn {background-color: #4CAF50;color: white;border: none;border-radius: 5px;padding: 12px;font-size: 16px;margin-top: 10px;width: 100%;
}.calculate-btn:active {background-color: #45a049;
}.result {margin-top: 25px;padding: 15px;background-color: #f9f9f9;border-radius: 8px;
}.result-header {margin-bottom: 15px;
}.result-title {font-size: 18px;font-weight: bold;color: #333;
}.result-item {display: flex;justify-content: space-between;margin-bottom: 10px;font-size: 16px;
}.result-value {font-weight: bold;color: #2196F3;
}
</style>

2. 配置页面路由

在 pages.json 中添加页面路由配置:

{"pages": [// ...其他页面配置{"path": "pages/fuel-calculator/fuel-calculator","style": {"navigationBarTitleText": "油耗计算器"}}]
}

3. 功能说明

这个 Uniapp 油耗计算器具有以下特点:

  1. 响应式设计:适配各种屏幕尺寸

  2. 输入验证:确保输入值为正数

  3. 计算结果

    • 加油量(升)

    • 百公里油耗(升/百公里)

    • 每公里油费(元)

  4. 用户体验优化

    • 计算后自动隐藏键盘

    • 错误输入提示

    • 清晰的结果展示

4. 使用方法

  1. 将代码添加到您的 Uniapp 项目中

  2. 通过路由跳转或导航栏访问油耗计算器页面

  3. 输入油价、加油金额和行驶里程

  4. 点击"计算油耗"按钮查看结果

5. 扩展建议

如果需要进一步增强功能,可以考虑:

  1. 添加历史记录功能,保存每次计算结果

  2. 实现多车管理,比较不同车辆的油耗

  3. 增加图表展示,可视化油耗变化趋势

  4. 添加分享功能,方便分享计算结果

这个组件已经包含了完整的计算逻辑和基本的UI界面,可以直接集成到您的Uniapp项目中使用。

相关文章:

  • CentOS 7 安装教程
  • VS-Code创建Vue3项目
  • 医院处方外流对接外部药房系统(合规python代码版)
  • ROS ROS2 机器人深度相机激光雷达多传感器标定工具箱
  • 编解码器vs容器
  • webpack理解与使用
  • 方德桌面操作系统V5.0-G23 vim无法复制粘贴内容
  • 李飞飞团队新作WorldScore:“世界生成”能力迎来统一评测,3D/4D/视频模型同台PK
  • 【论文精读】COLMAP-Free 3D Gaussian Splatting
  • [linux] vim 乱码
  • C++ 哈希表
  • Qt QML实现Windows桌面歌词动态播放效果
  • QtApplets-实现应用程序单例模式,防止重复运行
  • 2025年Q2(流动式)起重机司机考试题
  • 【Windows本地部署n8n工作流自动平台结合内网穿透远程在线访问】
  • Ubuntu利用docker搭建Java相关环境记录(二)
  • Vision Transformer项目分析与介绍
  • 压缩包网页预览(zip-html-preview)
  • Apache Atlas构建安装(Linux)
  • Python 深度学习 第8章 计算机视觉中的深度学习 - 卷积神经网络使用实例
  • 俄乌刚谈完美国便筹划与俄乌领导人通话,目的几何?
  • 国际博物馆日|航海博物馆:穿梭于海洋神话与造船工艺间
  • 世界高血压日|专家:高血压患者控制血压同时应注重心率管理
  • 美联储官员:美国经济增速可能放缓,现行关税政策仍将导致物价上涨
  • 《日出》华丽的悲凉,何赛飞和赵文瑄演绎出来了
  • 跨越三十年友情,61岁余隆和60岁齐默尔曼在上海再度合作