在 Vue 中使用 @import 导入样式文件时,可以通过在 vue.config.js 中的 css.loaderOptions 选项中设置 sasslessprependData 选项来添加请求头。

例如,在 vue.config.js 文件中添加以下配置:

module.exports = {
  css: {
    loaderOptions: {
      sass: {
        prependData: `@import '@/assets/styles/variables.scss';`,
        additionalData: `$http-header: 'Authorization: Bearer token';`
      },
      less: {
        prependData: `@import '@/assets/styles/variables.less';`,
        additionalData: `@http-header: 'Authorization: Bearer token';`
      }
    }
  }
};

prependData 中导入样式文件,并在文件中定义请求头变量。在 additionalData 中定义请求头变量,以便在样式中引用。

然后,在样式文件中使用请求头变量,如下所示:

// variables.scss
$theme-color: #007aff;
$http-header: '';

// style.scss
@import 'variables';

.header {
  background-color: $theme-color;
  &:before {
    content: $http-header;
  }
}
// variables.less
@theme-color: #007aff;
@http-header: '';

// style.less
@import 'variables';

.header {
  background-color: @theme-color;
  &:before {
    content: @http-header;
  }
}
Vue @import 导入样式文件添加请求头

原文地址: https://www.cveoy.top/t/topic/nu7U 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录