vue利用自定义插件和组件实现一个商品列表页,包含商品图、商品名、价格
时间: 2024-06-06 19:10:53 浏览: 10
1. 创建一个Vue插件,该插件可以获取商品列表数据并渲染到页面上。
```javascript
// 商品列表插件
const ProductList = {
install(Vue, options) {
Vue.prototype.$productList = [
{ id: 1, name: '商品1', price: 100, image: 'http://example.com/product1.jpg' },
{ id: 2, name: '商品2', price: 200, image: 'http://example.com/product2.jpg' },
{ id: 3, name: '商品3', price: 300, image: 'http://example.com/product3.jpg' },
{ id: 4, name: '商品4', price: 400, image: 'http://example.com/product4.jpg' },
];
}
}
// 注册插件
Vue.use(ProductList);
```
2. 创建一个商品列表组件,该组件可以接收商品列表数据并渲染到页面上。
```html
<template>
<div>
<div v-for="product in productList" :key="product.id" class="product">
<img :src="product.image" alt="商品图">
<div class="product-info">
<h3>{{ product.name }}</h3>
<p>¥{{ product.price }}</p>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'ProductList',
props: {
productList: {
type: Array,
required: true
}
}
}
</script>
<style>
.product {
display: flex;
align-items: center;
margin-bottom: 20px;
}
.product img {
width: 200px;
height: 200px;
margin-right: 20px;
}
.product h3 {
font-size: 18px;
margin-bottom: 10px;
}
.product p {
color: #f00;
font-size: 16px;
}
</style>
```
3. 在页面中使用商品列表组件,并通过商品列表插件获取商品列表数据,并将数据传递给商品列表组件。
```html
<template>
<div>
<product-list :product-list="$productList"></product-list>
</div>
</template>
<script>
import ProductList from '@/components/ProductList.vue';
export default {
name: 'ProductPage',
components: {
ProductList
}
}
</script>
```
这样就完成了一个商品列表页的实现,包含商品图、商品名、价格。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)