npm install --global @vue/cli // 全局安装脚手架
vue create toutiao-m // 创建项目
选择manually:自定义勾选特性配置,选择完毕之后,才会进入装包
等待安装完成
2、加入 Git 版本管理(省略)
3、调整初始目录结构
默认生成的目录结构不满足我们的开发需求,所以这里需要做一些自定义改动。
这里主要就是下面的两个工作:
- 删除初始化的默认文件
- 新增调整我们需要的目录结构
1、将 App.vue
修改为
2、将 router/index.js
修改为
3、删除
src/views/About.vue
src/views/Home.vue
src/components/HelloWorld.vue
src/assets/logo.png
4、创建以下几个目录
-
src/api
目录- 存储接口封装
-
src/utils
目录- 存储一些工具模块
-
src/styles
目录-
创建
index.less
文件,存储全局样式
-
在 main.js
中加载全局样式
调整之后的目录结构如下。
4、导入图标素材(省略)
5、配置到项目中使用
一种方式是将 SVG 图标 包装为 Vue 组件来使用 (opens new window),这种方式我们这里不做介绍
一种方式是将 SVG 制作为字体图标来使用,以下是项目中配置步骤
- 创建
src/styles/icon.less
文件, 将字体图标样式内容全部复制进去
- **在
src/styles/index.less
文件中导入字体样式 **
- 在
App.vue
中测试是否可以使用
6、引入 Vant 组件库
1、安装 Vant
yarn add vant@2.12.44
2、在 main.js
中加载注册 Vant 组件
3、查阅文档使用组件
Vant 3 - Mobile UI Components built on Vue
7、移动端 REM 适配
Vant 中的样式默认使用 px
作为单位,如果需要使用 rem
单位,推荐使用以下两个工具:
-
lib-flexible (opens new window)用于设置 rem 基准值
-
postcss-pxtorem (opens new window)是一款 postcss 插件,用于将单位转化为 rem
下面我们分别将这两个工具配置到项目中完成 REM 适配。
一、使用 lib-flexible (opens new window)动态设置 REM 基准值(html 标签的字体大小)
1、安装
# yarn add amfe-flexible
npm i amfe-flexible
1
2
2、然后在 main.js
中加载执行该模块
import 'amfe-flexible'
二、使用 postcss-pxtorem (opens new window)将 px
转为 rem
1、安装
# yarn add postcss-pxtorem@5.1.1 -D
# -D 是 --save-dev 的简写
npm install postcss-pxtorem@5.1.1 -D // 建议这个版本号
2、然后在项目根目录中创建 .postcssrc.js
或 postcss.config.js
文件
vant中有描述 关于适配的使用 https://vant-contrib.gitee.io/vant/#/zh-CN/advanced-usage#rem-bu-ju-gua-pei
module.exports = {
plugins: {
'postcss-pxtorem': {
rootValue: 37.5, // 如果将来是自己的设计图(750px),直接写75即可! 就可以设计图是多少px,css书写就写多少px
propList: ['*'],
},
},
};
但是上述配置只适用于vant内部的相关组件内容,我们自己书写的样式,并不是按照这个,我们希望设计图是多少px,就写多少px,故而修改内容如下
module.exports = {
plugins: {
// postcss-pxtorem 插件的版本需要 >= 5.0.0
'postcss-pxtorem': {
rootValue({ file }) { // 如果是vant的就按照375 尺寸, 如果是其他的就是按照750
return file.indexOf('vant') !== -1 ? 37.5 : 75; // rootValue 的值一般是 设计稿 1/10
},
propList: ['*'],
},
},
};
3、配置完毕,重新启动服务