Vue-CLI 3:我可以使用 CLI 编译非单文件组件 SASS 吗?
Vue-CLI 3: Can I have non-single file component SASS compiled by the CLI?
当使用 Vue-CLI 3 创建项目时,会创建一个 public/index.html。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Website</title>
</head>
<body>
<noscript>
<strong>We're sorry but the website doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
我想在头部添加一个外部 CSS 文件,但源文件在 SASS/SCSS 中。我想要这个文件的原因是围绕 Vue 应用程序 (<div id="app">/<div>
) 设置标记样式,例如 <body>
元素。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Website</title>
<link href="[I want to reference a compiled SCSS file here]" rel="stylesheet" type="text/css" />
</head>
<body>
<noscript>
<strong>We're sorry but the website doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
我在 CSS、https://cli.vuejs.org/guide/css.html 上阅读了 Vue-CLI 3 指南,但我的印象是该页面上的建议是将 SASS 集成到单个文件中组件(*.vue 文件),例如允许单个文件组件访问全局 SASS 变量。我能否获得 Vue-CLI 3 构建过程来编译一个 SASS 文件,该文件与任何 *.vue 文件都没有直接关系,但仍然是用于部署目的的 Vue 应用程序的一部分?
我找不到我的具体问题的答案。
不过,我确实在 Vue Loader Github 存储库中找到了讨论。
https://github.com/vuejs/vue-loader/issues/328#issuecomment-363165459
https://github.com/vuejs/vue-loader/issues/328#issuecomment-363189176
基本上,任何不包含 SASS 变量或混合的全局样式都可以导入到 App.vue 的样式块中,而无需 'scoped' 属性。
<style lang="scss">
@import './scss/main.scss';
</style>
任何需要访问 SASS 变量的 Vue 组件都需要对 Vue.config.js
进行小的更改
// vue.config.js
module.exports = {
css: {
loaderOptions: {
// pass options to sass-loader
sass: {
// @/ is an alias to src/
// so this assumes you have a file named `src/variables.scss`
data: `@import "@/variables.scss";`
}
}
}
}
当使用 Vue-CLI 3 创建项目时,会创建一个 public/index.html。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Website</title>
</head>
<body>
<noscript>
<strong>We're sorry but the website doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
我想在头部添加一个外部 CSS 文件,但源文件在 SASS/SCSS 中。我想要这个文件的原因是围绕 Vue 应用程序 (<div id="app">/<div>
) 设置标记样式,例如 <body>
元素。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Website</title>
<link href="[I want to reference a compiled SCSS file here]" rel="stylesheet" type="text/css" />
</head>
<body>
<noscript>
<strong>We're sorry but the website doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
我在 CSS、https://cli.vuejs.org/guide/css.html 上阅读了 Vue-CLI 3 指南,但我的印象是该页面上的建议是将 SASS 集成到单个文件中组件(*.vue 文件),例如允许单个文件组件访问全局 SASS 变量。我能否获得 Vue-CLI 3 构建过程来编译一个 SASS 文件,该文件与任何 *.vue 文件都没有直接关系,但仍然是用于部署目的的 Vue 应用程序的一部分?
我找不到我的具体问题的答案。
不过,我确实在 Vue Loader Github 存储库中找到了讨论。
https://github.com/vuejs/vue-loader/issues/328#issuecomment-363165459
https://github.com/vuejs/vue-loader/issues/328#issuecomment-363189176
基本上,任何不包含 SASS 变量或混合的全局样式都可以导入到 App.vue 的样式块中,而无需 'scoped' 属性。
<style lang="scss">
@import './scss/main.scss';
</style>
任何需要访问 SASS 变量的 Vue 组件都需要对 Vue.config.js
进行小的更改// vue.config.js
module.exports = {
css: {
loaderOptions: {
// pass options to sass-loader
sass: {
// @/ is an alias to src/
// so this assumes you have a file named `src/variables.scss`
data: `@import "@/variables.scss";`
}
}
}
}