用vue.js设计一个Web前端程序作品,要求至少包括首页、类目页、详情页、订单页、注册/登录页等
时间: 2023-08-12 16:06:10 浏览: 51
好的,以下是一个简单的基于Vue.js的Web前端程序作品示例,包括首页、类目页、详情页、订单页、注册/登录页等:
1. 首页
```html
<template>
<div>
<h1>欢迎来到我的商城!</h1>
<p>这里有各种商品供您选择购买。</p>
<ul>
<li v-for="item in products" :key="item.id">
<router-link :to="'/product/' + item.id">{{ item.name }}</router-link>
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
products: [
{ id: 1, name: '商品1', price: 10 },
{ id: 2, name: '商品2', price: 20 },
{ id: 3, name: '商品3', price: 30 }
]
}
}
}
</script>
```
2. 类目页
```html
<template>
<div>
<h1>{{ category }}</h1>
<p>这里有属于{{ category }}类别的商品。</p>
<ul>
<li v-for="item in products" :key="item.id">
<router-link :to="'/product/' + item.id">{{ item.name }}</router-link>
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
category: '电子产品',
products: [
{ id: 1, name: '手机', price: 2000 },
{ id: 2, name: '电脑', price: 5000 },
{ id: 3, name: '平板', price: 3000 }
]
}
}
}
</script>
```
3. 详情页
```html
<template>
<div>
<h1>{{ product.name }}</h1>
<p>价格:{{ product.price }}元</p>
<button @click="addToCart">加入购物车</button>
</div>
</template>
<script>
export default {
data() {
return {
product: null
}
},
mounted() {
// 从API获取商品信息
axios.get('/api/product/' + this.$route.params.id)
.then(response => {
this.product = response.data
})
.catch(error => {
console.log(error)
})
},
methods: {
addToCart() {
// 添加商品到购物车
console.log('添加成功!')
}
}
}
</script>
```
4. 订单页
```html
<template>
<div>
<h1>我的订单</h1>
<ul>
<li v-for="item in orders" :key="item.id">
{{ item.product }} - {{ item.price }}元
</li>
</ul>
<button @click="placeOrder">下单</button>
</div>
</template>
<script>
export default {
data() {
return {
orders: [
{ id: 1, product: '商品1', price: 10 },
{ id: 2, product: '商品2', price: 20 },
{ id: 3, product: '商品3', price: 30 }
]
}
},
methods: {
placeOrder() {
// 提交订单
console.log('下单成功!')
}
}
}
</script>
```
5. 注册/登录页
```html
<template>
<div>
<h1>注册/登录</h1>
<form>
<label for="username">用户名:</label>
<input type="text" id="username" v-model="username">
<br>
<label for="password">密码:</label>
<input type="password" id="password" v-model="password">
<br>
<button type="submit" @click.prevent="submitForm">提交</button>
</form>
<div v-if="message">{{ message }}</div>
</div>
</template>
<script>
export default {
data() {
return {
username: '',
password: '',
message: ''
}
},
methods: {
submitForm() {
// 发送登录/注册请求
axios.post('/api/login', {
username: this.username,
password: this.password
})
.then(response => {
// 登录/注册成功
this.message = '登录/注册成功!'
// 存储token
localStorage.setItem('token', response.data.token)
// 跳转到首页
this.$router.push('/')
})
.catch(error => {
// 登录/注册失败
this.message = '登录/注册失败,请检查用户名和密码!'
console.log(error)
})
}
}
}
</script>
```
注意:以上示例中涉及到的API请求需要根据实际情况进行编写,同时需要使用Vue Router进行页面路由控制。