如何在我的VUE应用程序中全局导入一个scss变量文件?

问题描述

我正在使用webpack的第3版开发新的应用程序,并根据官方webpack文档中的以下文章安装了sass-loader。

https://webpack.js.org/loaders/sass-loader/

这是我的package.json

{
  "name": "pricing-vue","version": "1.0.0","description": "A Vue.js project","author": "","private": true,"scripts": {
    "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js","start": "npm run dev","build": "node build/build.js"
  },"dependencies": {
    "axios": "^0.20.0","bootstrap": "^4.5.2","bootstrap-vue": "^2.16.0","style-resources-loader": "^1.3.3","vue": "^2.5.2","vue-router": "^3.0.1","vuex": "^3.5.1"
  },"devDependencies": {
    "autoprefixer": "^7.1.2","babel-core": "^6.22.1","babel-helper-vue-jsx-merge-props": "^2.0.3","babel-loader": "^7.1.1","babel-plugin-syntax-jsx": "^6.18.0","babel-plugin-transform-runtime": "^6.22.0","babel-plugin-transform-vue-jsx": "^3.5.0","babel-preset-env": "^1.3.2","babel-preset-stage-2": "^6.22.0","chalk": "^2.0.1","copy-webpack-plugin": "^4.0.1","css-loader": "^0.28.0","extract-text-webpack-plugin": "^3.0.0","file-loader": "^1.1.4","friendly-errors-webpack-plugin": "^1.6.1","html-webpack-plugin": "^2.30.1","node-notifier": "^5.1.2","optimize-css-assets-webpack-plugin": "^3.2.0","ora": "^1.2.0","portfinder": "^1.0.13","postcss-import": "^11.0.0","postcss-loader": "^2.0.8","postcss-url": "^7.2.1","rimraf": "^2.6.0","sass": "^1.26.10","sass-loader": "^7.3.1","semver": "^5.3.0","shelljs": "^0.7.6","uglifyjs-webpack-plugin": "^1.1.1","url-loader": "^0.5.8","vue-loader": "^13.3.0","vue-style-loader": "^3.0.1","vue-template-compiler": "^2.5.2","webpack": "^3.12.0","webpack-bundle-analyzer": "^2.9.0","webpack-dev-server": "^2.9.1","webpack-merge": "^4.1.0"
  },"engines": {
    "node": ">= 6.0.0","npm": ">= 3.0.0"
  },"browserslist": [
    "> 1%","last 2 versions","not ie <= 8"
  ]
}


[

vscode structure][1]

按照这些步骤,我在webpack.base.config.js中进行了修改,包括在“规则”部分中测试:/.s [ac] ss $ / i

这是我的webpack.base.config.js

'use strict'
const path = require('path')
const utils = require('./utils')
const config = require('../config')
const vueLoaderConfig = require('./vue-loader.conf')

function resolve (dir) {
  return path.join(__dirname,'..',dir)
}



module.exports = {
  context: path.resolve(__dirname,'../'),entry: {
    app: './src/main.js'
  },output: {
    path: config.build.assetsRoot,filename: '[name].js',publicPath: process.env.NODE_ENV === 'production'
      ? config.build.assetsPublicPath
      : config.dev.assetsPublicPath
  },resolve: {
    extensions: ['.js','.vue','.json'],alias: {
      'vue$': 'vue/dist/vue.esm.js','@': resolve('src'),}
  },module: {
    rules: [
      {
        test: /\.vue$/,loader: 'vue-loader',options: vueLoaderConfig
      },{
        test: /\.s[ac]ss$/i,use: [
          // Creates `style` nodes from JS strings
          'style-loader',// Translates CSS into CommonJS
          'css-loader',// Compiles Sass to CSS
          'sass-loader',],},{
        test: /\.js$/,loader: 'babel-loader',include: [resolve('src'),resolve('test'),resolve('node_modules/webpack-dev-server/client')]
      },{
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,loader: 'url-loader',options: {
          limit: 10000,name: utils.assetsPath('img/[name].[hash:7].[ext]')
        }
      },{
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,name: utils.assetsPath('media/[name].[hash:7].[ext]')
        }
      },{
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
        }
      }
    ]
  },node: {
    // prevent webpack from injecting useless setImmediate polyfill because Vue
    // source contains it (although only uses it if it's native).
    setImmediate: false,// prevent webpack from injecting mocks to Node native modules
    // that does not make sense for the client
    dgram: 'empty',fs: 'empty',net: 'empty',tls: 'empty',child_process: 'empty'
  }
}

但是当我在main.js中导入包含scss变量的文件时,编译失败,提示它找不到我要引用的模块。 在这里,我如何导入_variables.scss

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import BootstrapVue from 'bootstrap-vue'
import 'bootstrap/dist/css/bootstrap.min.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
import axios from 'axios'
import Config from './config.js'
import './styles/_variables.scss'



Vue.config.productionTip = false

Vue.use(BootstrapVue)

axios.defaults.baseURL = Config.BASE_URL_API
axios.defaults.headers.get['Accepts'] = 'application/json'

const reqInterceptor = axios.interceptors.request.use(config => {
  console.log('Request Interceptor',config)
  return config
})
const resInterceptor = axios.interceptors.response.use(res => {
  console.log('Response Interceptor',res)
  return res
})

axios.interceptors.request.eject(reqInterceptor)
axios.interceptors.response.eject(resInterceptor)


/* eslint-disable no-new */
new Vue({
  el: '#app',router,store,components: { App },template: '<App/>'
})

这是我的VSC控制台中的错误:

This relative module was not found:

* ./styles/_variables.scss in ./src/main.js
Error from chokidar (C:\): Error: EBUSY: resource busy or locked,lstat 'C:\hiberfil.sys'
Error from chokidar (C:\): Error: EBUSY: resource busy or locked,lstat 'C:\pagefile.sys'
Error from chokidar (C:\): Error: EBUSY: resource busy or locked,lstat 'C:\swapfile.sys'
(node:10768) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory,stat 'C:\Users\All Users'
(node:10768) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
(node:10768) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory,stat 'C:\Users\Default User'
(node:10768) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). (rejection id: 4)

我在做什么错? 预先感谢您的时间和帮助

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...