如何搭建和发布一个 Vue 组件库

  • What is the npm name of your library?  (名称在 npm 中必须是唯一的,这里我使用的是  brian-component-lib

  • Will this library be written in JavaScript or TypeScript?  JavaScript(可以放心地选择 TypeScript,只要你清楚自己的选择)

  • Enter a location to save the library files:  (项目的文件夹名称,默认为之前步骤设置的 npm 名称,所以可以直接按 enter 取默认值。)

设置完成之后,进入项目目录并执行 npm install。

cd path/to/my-component-or-lib

npm install

完成之后,选择一个编辑器来打开项目目录。

如上所述,在  /src/lib-components  文件夹中可以看到,一个示例 Vue 组件已经为我们构建好了。要查看效果,可以运行  npm run serve  命令,然后在浏览器中访问  http://localhost:8080/。

开始添加我们的自定义组件。在这个示例中,我打算模拟 freeCodeCamp 的任务介绍部分的按钮,所以在  lib-components  文件夹中新建一个名为  FccButton.vue  的 Vue 文件。

  • 26
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的商品发布和购买页面的示例,代码基于Vant UIVue.js框架实现。 首先,创建一个Vue.js项目,并安装Vant UI: ``` vue create my-project cd my-project npm install vant ``` 接着,在`src/main.js`文件中引入Vant UI和样式: ```javascript import { createApp } from 'vue' import App from './App.vue' import 'vant/lib/index.css' import { Button, Form, Field, Uploader, Dialog, SubmitBar, Card } from 'vant' const app = createApp(App) app.use(Button) app.use(Form) app.use(Field) app.use(Uploader) app.use(Dialog) app.use(SubmitBar) app.use(Card) app.mount('#app') ``` 然后,在`src/App.vue`文件中编写商品发布和购买页面的代码: ```html <template> <div class="container"> <van-form v-model="form" label-width="80px"> <van-field v-model="form.name" label="商品名称" required /> <van-field v-model="form.description" label="商品描述" type="textarea" rows="3" required /> <van-field v-model="form.price" label="商品价格" type="number" required /> <van-field v-model="form.category" label="商品分类" required /> <van-uploader v-model="form.images" label="商品图片" multiple accept="image/*" :max-count="3" /> <van-button type="primary" @click="submitForm">发布</van-button> </van-form> <van-card v-for="(item, index) in goods" :key="index" :title="item.name" :price="item.price" :desc="item.description" :thumb="item.images[0].url"> <template #footer> <van-button type="primary" @click="showDialog(item)">购买</van-button> </template> </van-card> <van-dialog v-model="showBuyDialog" title="购买确认" :confirm-button-text="'确认购买(¥' + selectedGoods.price.toFixed(2) + ')'" @confirm="buyGoods"> <p>商品名称:{{ selectedGoods.name }}</p> <p>商品价格:¥{{ selectedGoods.price.toFixed(2) }}</p> <p>商品描述:{{ selectedGoods.description }}</p> <p>商品分类:{{ selectedGoods.category }}</p> <p>商品图片:</p> <van-image v-for="(image, index) in selectedGoods.images" :key="index" :src="image.url" width="80" height="80" fit="cover" /> </van-dialog> <van-submit-bar :price="totalPrice" button-text="去结算" @submit="submitOrder" /> </div> </template> <script> import { reactive } from 'vue' export default { name: 'App', setup() { const form = reactive({ name: '', description: '', price: '', category: '', images: [] }) const goods = reactive([ { name: '商品1', price: 100, description: '商品1的描述', category: '分类1', images: [ { url: 'https://img.yzcdn.cn/vant/ipad.jpeg' }, { url: 'https://img.yzcdn.cn/vant/ipad.jpeg' }, { url: 'https://img.yzcdn.cn/vant/ipad.jpeg' } ] }, { name: '商品2', price: 200, description: '商品2的描述', category: '分类2', images: [ { url: 'https://img.yzcdn.cn/vant/ipad.jpeg' }, { url: 'https://img.yzcdn.cn/vant/ipad.jpeg' } ] } ]) const selectedGoods = reactive({ name: '', price: '', description: '', category: '', images: [] }) let showBuyDialog = false const totalPrice = computed(() => { return selectedGoods.price }) const submitForm = () => { console.log(form) // TODO: 发送表单数据到后端API } const showDialog = (item) => { selectedGoods.name = item.name selectedGoods.price = item.price selectedGoods.description = item.description selectedGoods.category = item.category selectedGoods.images = item.images showBuyDialog = true } const buyGoods = () => { console.log(selectedGoods) // TODO: 发送购买请求到后端API showBuyDialog = false } const submitOrder = () => { console.log('提交订单') // TODO: 提交订单到后端API } return { form, goods, selectedGoods, showBuyDialog, totalPrice, submitForm, showDialog, buyGoods, submitOrder } } } </script> <style> .container { padding: 16px; } </style> ``` 在上面的代码中,我们使用`van-form`组件和`van-field`组件实现商品发布表单,使用`van-uploader`组件实现图片上传功能,使用`van-card`组件展示已发布的商品列表。在购买商品时,我们使用`van-dialog`组件展示商品详情,并使用`van-submit-bar`组件实现结算功能。 注意:在实际开发中,你需要根据实际需求对代码进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值