准备 sass
在style
标签中加入
// index/index.vue
<style lang="scss" scoped>
.content {
background: red;
}
</style>
控制台这时候会报错,node-sass不存在
解决方案:安装yarn add sass sass-loader -D
再次运行,报错没有了,说明我们可以直接使用sass来编写我们的style样式了。
:::warning
如果 node 版本小于 16 ,sass-loader 请使用低于 @11.0.0 的版本,sass-loader@11.0.0 不支持 vue@2.6.12(opens new window)如果 node 版本大于 16 , sass-loader 建议使用 v8.x 版本
:::
安装uni-ui
配置easycom
使用 npm 安装好 uni-ui 之后,需要配置 easycom 规则,让 npm 安装的组件支持 easycom
打开项目根目录下的 pages.json 并添加 easycom 节点:
// pages.json
{
"easycom": {
"autoscan": true,
"custom": {
// uni-ui 规则如下配置
"^uni-(.*)": "@dcloudio/uni-ui/lib/uni-$1/uni-$1.vue"
}
},
// 其他内容
pages:[
// ...
]
}
配置vue.config.js
:::warning 注意 cli 项目默认是不编译 node_modules 下的组件的,导致条件编译等功能失效 ,导致组件异常 需要在根目录创建 vue.config.js 文件 ,增加 @dcloudio/uni-ui 包的编译即可正常 :::
// vue.config.js
module.exports = {
transpileDependencies:['@dcloudio/uni-ui']
}
在 template 中使用组件:
<uni-badge text="1"></uni-badge>
<uni-badge text="2" type="success" @click="bindClick"></uni-badge>
<uni-badge text="3" type="primary" :inverted="true"></uni-badge>
可以看到效果如下:
uni.scss使用
uni.scss
文件里面定义一些默认的变量,我们可以直接在样式当时使用,无需单独引入,比如下面这样的
<style lang="scss" scoped>
.content {
.title {
color: $uni-color-primary;
}
}
</style>