[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
前提是已经安装了node.js.
首先得新建一个Vue项目:(这里只是一个大概的方法)
1.打开命令窗口,全局安装脚手架构建工具:
npm install -g vue-cli 回车
2.命令行中运行命令 vue init webpack vantappdemo后回车(初始化一个完整版的项目)
。解释一下这个命令,这个命令的意思是初始化一个项目。最后的是项目名称自己根据项目内容来起
vue init webpack vantappdemo
成功初始化后:
像上面这个样子就是一个建好的Vue项目了。
接下来就是在vue-cli脚手架中的vant部分了。
1.首先是安装Vant:
在命令行输入命令:npm install vant --save
2.引入组件:
推荐的方法是使用 babel-plugin-import
babel-plugin-import 是一款 babel 插件,它会在编译过程中将 import 的写法自动转换为按需引入的方式
# 安装 babel-plugin-import 插件 npm i babel-plugin-import -D
然后要在.babelrc中配置plugins中插入下面的组件:
// 在 .babelrc 或 babel-loader 中添加插件配置 // 注意:webpack 1 无需设置 libraryDirectory {
"plugins": [ ["import", { "libraryName": "vant", "libraryDirectory": "es",
"style": true }] ] }
首先打开新建的项目我们会看到原本的.babelrc文件长这个样子:
{ "presets": [ ["env", { "modules": false, "targets": { "browsers": ["> 1%",
"last 2 versions", "not ie <= 8"] } }], "stage-2" ], "plugins":
["transform-vue-jsx", "transform-runtime"] }
插入组件配置就是这个样子:
到了这里就考虑怎么使用组件了:
不在src/main.js里加入代码,而在需要的组件/页面中单独引入(按需引入):
其中里面的name表示的是这个组件的名字,如果是页面的话就没有这个name了:
也可以全部的组件都引入: