美文网首页
脚手架搭建的vue2项目中使用sass

脚手架搭建的vue2项目中使用sass

作者: 安素白 | 来源:发表于2022-04-13 10:35 被阅读0次

1.基础配置
安装环境:

npm i node-sass sass-loader style-loader -D

安装成功就可以在项目中使用sass

<style lang="scss">
//css样式
</style>

全局注册scss:

npm i sass-resources-loader -D

vue.config.js配置

module.exports = {
    chainWebpack: (config) => {
        const oneOfsMap = config.module.rule('scss').oneOfs.store
        oneOfsMap.forEach((item) => {
            item.use('sass-resources-loader')
                .loader('sass-resources-loader')
                .options({
                    resources: [
                        './src/assets/reset.scss', //全局scss文件地址
                        './src/assets/mixin.scss'
                    ]
                })
                .end()
        })
    }
}

然后重新运行,就可使用
2.换肤小案例:

//reset.scss
$blueList: #7760ec #647bfd #7f8fea;

$redList: #fb0067 #ec146d #ea7fab;

$greenList: #0ad84c #39dc6d #7feaa1;
//mixin.scss
@mixin btn_color($index) {
    [data-theme="blue"] & {
        background-color: nth($blueList , $index);
    }
    [data-theme="red"] & {
        background-color: nth($redList , $index);
    }
    [data-theme="green"] & {
        background-color: nth($greenList , $index);
    }
}
// skinChange.vue
<template>
    <div>
        <div class="icon" @click="showSkin()"><i class="iconfont" :style="{color:skins[skinIndex].color}">&#xeeb1;</i></div>
        <div class="skins" v-if="isshowskin">
            <div class="skin"
                v-for="(sitem,sindex) in skins"
                :key="'skin'+sindex" 
                :style="{backgroundColor:sitem.color}"
                @click="changeIndex(sindex)"
                :class="{sative:skinIndex===sindex}"
                >
                <div class="mask"></div>
            </div>
        </div>
        <div class="btns">
            <div class="btn"
                v-for="(bitem, bindex) in btns"
                :key="'btn'+bindex"
                :class="'btn-'+(bindex+1)">{{bitem}}</div>
        </div>
    </div>
</template>

<script>
export default {
    name:'skin-change',
    data() {
        return {
            isshowskin: false,
            skins:[
                {
                    color:'#7f8fea',
                    name:'blue'
                },{
                    color:'#ea7fab',
                    name:'red'
                },{
                    color:'#7feaa1',
                    name:'green'
                }
            ],
            skinIndex: 0,
            btns:['看看', '其他', '零零']
        }
    },
    computed:{
    },
    methods:{
        showSkin(){
            this.isshowskin = true
        },
        changeIndex(index) {
            this.skinIndex = index
            window.document.documentElement.setAttribute('data-theme', this.skins[index].name)
        }
    },
    mounted() {
        window.document.documentElement.setAttribute('data-theme', this.skins[this.skinIndex].name)
    }
}
</script>

<style lang="scss">
    .icon{
        i{
            color: #7f8fea;
            font-size: 30px;
        }
    }
    .skins{
        width: 160px;
        height: 60px;
        display: grid;
        padding: 10px;
        grid-template-columns: repeat(3, 1fr);
        grid-auto-rows: 40px;
        gap: 10px;
        box-shadow: 0 0 12px #dedede;
        .skin{
            position: relative;
            .mask{
                position: absolute;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                background-color: rgba(255,255,255,.6);
                opacity: 0;
            }
            &.sative{
                .mask{
                    opacity: 1;
                }
            }
        }
    }
    .btns{
        margin-top: 50px;
        display: flex;
        .btn{
            padding: 10px 24px;
            border-radius: 6px;
            margin-left: 10px;
            color: #fff;
        }
        @for $i from 1 through 3 {
            .btn-#{$i} { 
                @include btn_color($i);
            }
        }
    }
</style>

最后看一下展示效果吧


skins.jpg

相关文章

  • 脚手架搭建的vue2项目中使用sass

    1.基础配置安装环境: 安装成功就可以在项目中使用sass 全局注册scss: vue.config.js配置 然...

  • vue-cli中配置sass

    vue2开发中,如果你习惯用sass,那就利用脚手架配置sass吧,脚手架真好安利 安装对应依赖node模块: 打...

  • vue 安装node-sass 报错 this.getRes

    装node-scss报错 在搭建vue脚手架 或者是在vue项目中,想使用sass的功能, 安装完成后,运行时出现...

  • Module build failed: TypeError:

    在搭建vue脚手架 或者是在vue项目中,想使用sass的功能,但是安装完成后,运行时出现了错误Modele bu...

  • Vue引入sass并配置全局变量

    Vue引入sass并配置全局变量 引入sass 首先使用官方提供的脚手架vue-cli进行搭建框架,这里就不做说明...

  • sass 安装 使用 报错

    安装脚手架后我我想在项目中使用sass,于是直接输入命令 npm i sass 然后。。。。。各种不成功,然后尝试...

  • 搭建React项目

    为了在React项目中使用ES6,JSX,并且实现热刷新,使用React官方脚手架进行项目搭建。使用脚手架的原因:...

  • vue2.0项目中引入sass、less

    一、vue2.0项目中引入sass预编译 (1)安装依赖 vue项目中想要使用sass,需要安装上node-sas...

  • Vue3中使用Pinia

    Vue2 中使用Vuex进行状态管理,在Vue3中,引入了Pinia,如果使用Vue3的脚手架搭建项目,其中包含了...

  • SASS - 简介

    SASS – 简介 SASS – 环境搭建 SASS – 使用Sass程序 SASS – SASS – 语法 SA...

网友评论

      本文标题:脚手架搭建的vue2项目中使用sass

      本文链接:https://www.haomeiwen.com/subject/txltertx.html