首先安装好sass

npm install sass-loader node-sass vue-style-loader --D

这个时候你打开build文件夹下面的webpack.base.config.js
把里面的module改成这样

module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: vueLoaderConfig
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: [resolve('src'), resolve('test')]
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('img/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
        }
      },
      {  //从这一段上面是默认的!不用改!下面是没有的需要你手动添加,相当于是编译识别sass!
        test: /\.scss$/,
        loaders: ["style", "css", "sass"]
      }
    ]
  }

index.html添加mate标签

<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1,minimum-scale=1,user-scalable=no">

app.vue文件设置转换方法

<script>
export default {
  name: 'App',
 
  created(){},
  methods:{

  },
  mounted(){
      window.onload=function(){
        //获取屏幕宽度
       let htmlwidth=document.documentElement.clientWidth || document.body.clientWidth;
        // 获取html的dom
        let htmldom=document.getElementsByTagName('html')[0];
        //设置html的font-size
        htmldom.style.fontSize=htmlwidth/10+'px';
        window.addEventListener('resize',(e)=>{
           let htmlwidth=document.documentElement.clientWidth || document.body.clientWidth;
              htmldom.style.fontSize=htmlwidth/10+'px';
        })
      }
  }
}
</script>

调用,在需要用到sass的地方添加lang=scss

<style lang="scss">
@function px2rem($px){
  $rem :37.5px;
  @return ($px/ $rem) + rem;
}

</style>

Logo

前往低代码交流专区

更多推荐