如何使用 Deepseek 寫的uniapp油耗計算器

下面是一個基于 Uniapp 的油耗計算器實現(xiàn),包含 Vue 組件和頁面代碼。
1. 創(chuàng)建頁面文件
在 pages 目錄下創(chuàng)建 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">計算結(jié)果</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) {
// 確保輸入是正數(shù)
if (this[field] < 0) {
this[field] = ''
}
},
calculate() {
// 驗證輸入
if (!this.price || !this.money || !this.distance) {
uni.showToast({
title: '請?zhí)顚懲暾畔?,
icon: 'none'
})
return
}
if (this.price <= 0 || this.money <= 0 || this.distance <= 0) {
uni.showToast({
title: '請輸入大于0的數(shù)值',
icon: 'none'
})
return
}
// 計算
this.fuel = this.money / this.price
this.consumption = (this.fuel / this.distance) * 100
this.costPerKm = this.money / this.distance
this.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 油耗計算器具有以下特點:
- 響應式設計:適配各種屏幕尺寸
- 輸入驗證:確保輸入值為正數(shù)
- 計算結(jié)果:
- 加油量(升)
- 百公里油耗(升/百公里)
- 每公里油費(元)
- 用戶體驗優(yōu)化:
- 計算后自動隱藏鍵盤
- 錯誤輸入提示
- 清晰的結(jié)果展示
4. 使用方法
- 將代碼添加到您的 Uniapp 項目中
- 通過路由跳轉(zhuǎn)或?qū)Ш綑谠L問油耗計算器頁面
- 輸入油價、加油金額和行駛里程
- 點擊"計算油耗"按鈕查看結(jié)果
5. 擴展建議
如果需要進一步增強功能,可以考慮:
- 添加歷史記錄功能,保存每次計算結(jié)果
- 實現(xiàn)多車管理,比較不同車輛的油耗
- 增加圖表展示,可視化油耗變化趨勢
- 添加分享功能,方便分享計算結(jié)果
這個組件已經(jīng)包含了完整的計算邏輯和基本的UI界面,可以直接集成到您的Uniapp項目中使用。
到此這篇關于如何使用 Deepseek 寫的uniapp油耗計算器的文章就介紹到這了,更多相關Deepseek uniapp油耗計算器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue使用watch監(jiān)聽一個對象中的屬性的實現(xiàn)方法
這篇文章主要介紹了Vue使用watch監(jiān)聽一個對象中的屬性的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05
vue click.stop阻止點擊事件繼續(xù)傳播的方法
今天小編就為大家分享一篇vue click.stop阻止點擊事件繼續(xù)傳播的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue+ElementUI?實現(xiàn)分頁功能-mysql數(shù)據(jù)
這篇文章主要介紹了Vue+ElementUI?實現(xiàn)分頁查詢-mysql數(shù)據(jù),當數(shù)據(jù)庫中數(shù)據(jù)比較多時,就每次只查詢一部分來緩解服務器和頁面壓力。這里使用elementui的?Pagination?分頁?組件,配合mysql的limit語句,實現(xiàn)分頁查詢mysql數(shù)據(jù),下面來看看具體實現(xiàn)過程,希望對大家學習有所幫助2021-12-12
Vue.js實戰(zhàn)之組件之間的數(shù)據(jù)傳遞
這篇文章主要介紹了Vue.js實戰(zhàn)之組件之間的數(shù)據(jù)傳遞的相關資料,文中通過示例代碼和圖文介紹的非常詳細,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-04-04

