vue实现快递查询
实现思路
通过调用第三方快递查询API接口,结合Vue.js框架实现快递单号查询功能。需要用户输入快递单号和选择快递公司,点击查询后显示物流信息。

准备工作
注册快递查询API服务(如快递100、聚合数据等),获取API密钥。以快递100为例,其免费接口支持实时查询。
安装依赖
在Vue项目中安装axios用于HTTP请求:
npm install axios
核心代码实现
模板部分
<template>
<div class="express-query">
<select v-model="selectedCompany">
<option v-for="company in companies" :value="company.code">{{ company.name }}</option>
</select>
<input v-model="trackingNumber" placeholder="输入快递单号">
<button @click="queryExpress">查询</button>
<div v-if="result">
<h3>物流信息</h3>
<ul>
<li v-for="item in result.data" :key="item.time">
{{ item.time }} - {{ item.context }}
</li>
</ul>
</div>
</div>
</template>
脚本部分
<script>
import axios from 'axios';
export default {
data() {
return {
trackingNumber: '',
selectedCompany: 'yuantong', // 默认圆通
companies: [
{ code: 'yuantong', name: '圆通快递' },
{ code: 'shentong', name: '申通快递' },
{ code: 'zhongtong', name: '中通快递' }
],
result: null
}
},
methods: {
async queryExpress() {
try {
const response = await axios.get(
`https://api.kuaidi100.com/v1/query?type=${this.selectedCompany}&postid=${this.trackingNumber}`,
{
headers: {
'Authorization': 'YOUR_API_KEY' // 替换为实际API密钥
}
}
);
this.result = response.data;
} catch (error) {
console.error('查询失败:', error);
alert('查询失败,请检查单号和快递公司');
}
}
}
}
</script>
样式部分
<style scoped>
.express-query {
max-width: 600px;
margin: 0 auto;
padding: 20px;
}
select, input {
padding: 8px;
margin-right: 10px;
}
button {
padding: 8px 16px;
background: #42b983;
color: white;
border: none;
}
ul {
list-style: none;
padding: 0;
}
li {
padding: 8px 0;
border-bottom: 1px solid #eee;
}
</style>
注意事项
- 需要替换代码中的
YOUR_API_KEY为实际申请的API密钥 - 快递公司代码需与API文档保持一致
- 免费API通常有调用次数限制,生产环境建议购买商业套餐
- 实际开发中应添加防抖/节流处理频繁查询
- 可增加加载状态提示提升用户体验
扩展功能建议
- 添加自动识别快递公司功能
- 实现物流轨迹地图可视化
- 增加查询历史记录功能
- 支持扫描二维码输入快递单号







