当前位置:实例文章 » HTML/CSS实例» [文章]Vue项目实战失物招领

Vue项目实战失物招领

发布人:shili8 发布时间:2025-03-02 18:55 阅读次数:0

**Vue项目实战:失物招领系统**

在日常生活中,失物是非常常见的问题。尤其是在大型公共场所或活动中,人们容易丢失自己的东西,如手机、钱包、手表等。如果能够快速找到这些失物,可以避免很多不必要的麻烦和损失。

本文将介绍如何使用 Vue.js 构建一个失物招领系统。这个系统可以帮助用户快速找到自己丢失的东西,并且提供一个平台让失物的主人联系到失物的发现者。

**项目需求**

1. 用户注册和登录功能2. 失物登记功能,包括失物类型、描述、图片等信息3. 招领功能,让用户可以搜索并找到自己丢失的东西4. 联系功能,让失物的主人联系到失物的发现者**项目结构**

bashproject/
public/
index.htmlsrc/
App.vuemain.jsregister.vuelogin.vuelost-item.vuefound-item.vuecontact.vuecomponents/
LostItem.vueFoundItem.vueContact.vueutils/
api.jsstore.jsrouter.jspackage.json


**用户注册和登录功能**

首先,我们需要实现用户的注册和登录功能。我们使用 Vue 的 Composition API 来管理状态。

javascript// src/register.vue<template>
 <div>
 <h1>Register</h1>
 <form @submit.prevent="register">
 <input type="text" v-model="username" placeholder="Username">
 

 <input type="password" v-model="password" placeholder="Password">
 

 <button type="submit">Register</button>
 </form>
 </div>
</template>

<script>
import { ref } from 'vue'

export default {
 setup() {
 const username = ref('')
 const password = ref('')

 const register = async () => {
 try {
 await fetch('/api/register', {
 method: 'POST',
 headers: { 'Content-Type': 'application/json' },
 body: JSON.stringify({ username, password }),
 })
 console.log('Registered successfully!')
 } catch (error) {
 console.error(error)
 }
 }

 return { username, password, register }
 },
}
</script>


javascript// src/login.vue<template>
 <div>
 <h1>Login</h1>
 <form @submit.prevent="login">
 <input type="text" v-model="username" placeholder="Username">
 

 <input type="password" v-model="password" placeholder="Password">
 

 <button type="submit">Login</button>
 </form>
 </div>
</template>

<script>
import { ref } from 'vue'

export default {
 setup() {
 const username = ref('')
 const password = ref('')

 const login = async () => {
 try {
 await fetch('/api/login', {
 method: 'POST',
 headers: { 'Content-Type': 'application/json' },
 body: JSON.stringify({ username, password }),
 })
 console.log('Logged in successfully!')
 } catch (error) {
 console.error(error)
 }
 }

 return { username, password, login }
 },
}
</script>


**失物登记功能**

下一步,我们需要实现失物的登记功能。我们使用 Vue 的 Composition API 来管理状态。

javascript// src/lost-item.vue<template>
 <div>
 <h1>Lost Item</h1>
 <form @submit.prevent="register">
 <input type="text" v-model="name" placeholder="Name">
 

 <textarea v-model="description" placeholder="Description"></textarea>
 

 <input type="file" @change="uploadImage" ref="imageInput">
 <img :src="imageUrl" alt="" v-if="imageUrl">
 <button type="submit">Register</button>
 </form>
 </div>
</template>

<script>
import { ref } from 'vue'

export default {
 setup() {
 const name = ref('')
 const description = ref('')
 const imageUrl = ref('')

 const register = async () => {
 try {
 await fetch('/api/lost-item', {
 method: 'POST',
 headers: { 'Content-Type': 'application/json' },
 body: JSON.stringify({ name, description }),
 })
 console.log('Registered successfully!')
 } catch (error) {
 console.error(error)
 }
 }

 const uploadImage = async () => {
 try {
 const file = await this.$refs.imageInput.files[0]
 const formData = new FormData()
 formData.append('image', file)

 const response = await fetch('/api/upload-image', {
 method: 'POST',
 body: formData,
 })

 if (response.ok) {
 const data = await response.json()
 imageUrl.value = data.imageUrl } else {
 console.error(response.statusText)
 }
 } catch (error) {
 console.error(error)
 }
 }

 return { name, description, register, uploadImage, imageUrl }
 },
}
</script>


**招领功能**

下一步,我们需要实现招领的功能。我们使用 Vue 的 Composition API 来管理状态。

javascript// src/found-item.vue<template>
 <div>
 <h1>Found Item</h1>
 <form @submit.prevent="search">
 <input type="text" v-model="name" placeholder="Name">
 

 <button type="submit">Search</button>
 </form>
 </div>
</template>

<script>
import { ref } from 'vue'

export default {
 setup() {
 const name = ref('')

 const search = async () => {
 try {
 await fetch('/api/found-item', {
 method: 'POST',
 headers: { 'Content-Type': 'application/json' },
 body: JSON.stringify({ name }),
 })
 console.log('Searched successfully!')
 } catch (error) {
 console.error(error)
 }
 }

 return { name, search }
 },
}
</script>


**联系功能**

最后,我们需要实现联系的功能。我们使用 Vue 的 Composition API 来管理状态。

javascript// src/contact.vue<template>
 <div>
 <h1>Contact</h1>
 <form @submit.prevent="contact">
 <input type="text" v-model="name" placeholder="Name">
 

 <input type="email" v-model="email" placeholder="Email">
 

 <textarea v-model="message" placeholder="Message"></textarea>
 

 <button type="submit">Contact</button>
 </form>
 </div>
</template>

<script>
import { ref } from 'vue'

export default {
 setup() {
 const name = ref('')
 const email = ref('')
 const message = ref('')

 const contact = async () => {
 try {
 await fetch('/api/contact', {
 method: 'POST',
 headers: { 'Content-Type': 'application/json' },
 body: JSON.stringify({ name, email, message }),
 })
 console.log('Contacted successfully!')
 } catch (error) {
 console.error(error)
 }
 }

 return { name, email, message, contact }
 },
}
</script>


**总结**

本文介绍了如何使用 Vue.js 构建一个失物招领系统。这个系统可以帮助用户快速找到自己丢失的东西,并且提供一个平台让失物的主人联系到失物的发现者。

我们使用 Vue 的 Composition API 来管理状态,实现了用户注册和登录功能、失物登记功能、招领功能和联系功能。

希望本文对你有所帮助!

其他信息

其他资源

Top