当我在我的 Vue 组件<style>
标签中包含 Sass 文件(node_modules
(时,该文件位于,但该文件的任何相关模块都没有。 一切似乎都配置正确,但我显然错过了一些东西。 什么给?
我尝试安装/卸载各种 Webpack 加载器(包括css-loader
(,删除并重新安装node_modules
,尝试各种 PostCSS 配置,但到目前为止没有任何进展。
提前谢谢。
Vue 组件 (IconicIcon.vue
(:
<template>
<div>
<span class="oi oi-icon-name" :title="icon" aria-hidden="true"></span>
</div>
</template>
<script>
export default {
name: "iconic-icon",
props: {
icon: String
}
};
</script>
<style scoped lang="scss">
$icon-font-path: "~open-iconic/font/css/fonts";
@import "~open-iconic/font/css/open-iconic-bootstrap.scss";
</style>
open-iconic-bootstrap.scss
:
/* Bootstrap */
/* Override Bootstrap default variable */
$icon-font-path: '../fonts/' !default;
@font-face {
font-family: 'Icons';
src: url('#{$icon-font-path}open-iconic.eot');
src: url('#{$icon-font-path}open-iconic.eot?#iconic-sm') format('embedded-opentype'), url('#{$icon-font-path}open-iconic.woff') format('woff'), url('#{$icon-font-path}open-iconic.ttf') format('truetype'), url('#{$icon-font-path}open-iconic.svg#iconic-sm') format('svg');
font-weight: normal;
font-style: normal;
}
... (snip)
相关文件结构:
root/
src/
components/
IconicIcon.vue
node_modules/
open-iconic/
font/
css/
open-iconic-bootstrap.scss
fonts/
open-iconic.eot
open-iconic.otf
open-iconic.svg
open-iconic.ttf
open-iconic.woff
错误:
WAIT Compiling... 8:37:49 PM
98% after emitting CopyPlugin
ERROR Failed to compile with 4 errors 8:37:50 PM
These relative modules were not found:
* ../fonts/open-iconic.eot in ./node_modules/css-loader/dist/cjs.js??ref--8-oneOf-1-1!./node_modules/vue-loader/lib/loaders/stylePost
Loader.js!./node_modules/postcss-loader/src??ref--8-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--8-oneOf-1-3!./node_modules
/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/components/IconicIcon.vue?vue&type=style&
index=0&id=468ee29c&scoped=true&lang=scss&
* ../fonts/open-iconic.svg in ./node_modules/css-loader/dist/cjs.js??ref--8-oneOf-1-1!./node_modules/vue-loader/lib/loaders/stylePost
Loader.js!./node_modules/postcss-loader/src??ref--8-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--8-oneOf-1-3!./node_modules
/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/components/IconicIcon.vue?vue&type=style&
index=0&id=468ee29c&scoped=true&lang=scss&
* ../fonts/open-iconic.ttf in ./node_modules/css-loader/dist/cjs.js??ref--8-oneOf-1-1!./node_modules/vue-loader/lib/loaders/stylePost
Loader.js!./node_modules/postcss-loader/src??ref--8-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--8-oneOf-1-3!./node_modules
/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/components/IconicIcon.vue?vue&type=style&
index=0&id=468ee29c&scoped=true&lang=scss&
* ../fonts/open-iconic.woff in ./node_modules/css-loader/dist/cjs.js??ref--8-oneOf-1-1!./node_modules/vue-loader/lib/loaders/stylePos
tLoader.js!./node_modules/postcss-loader/src??ref--8-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--8-oneOf-1-3!./node_module
s/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/components/IconicIcon.vue?vue&type=style
&index=0&id=468ee29c&scoped=true&lang=scss&
package.json
:
{
"name": "myapp",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "concurrently -r -k -n doc,nod,vue -c magenta,green,cyan "docker-compose up" "nodemon server/app.js" "vue-cli-service serve"",
"build": "vue-cli-service build",
"start": "node server/app.js",
"test:unit": "vue-cli-service test:unit",
"test:e2e": "vue-cli-service test:e2e"
},
"dependencies": {
"apollo-server": "^2.9.15",
"babel-loader": "^8.0.6",
"bootstrap": "^4.4.1",
"core-js": "^3.4.3",
"eslint": "^6.8.0",
"graphql": "^14.5.8",
"graphql-type-json": "^0.2.1",
"jquery": "^3.4.1",
"js-yaml-loader": "^1.2.2",
"leaflet": "^1.6.0",
"lowdb": "^1.0.0",
"mkdirp": "^0.5.1",
"mongodb": "^3.4.1",
"open-iconic": "^1.1.1",
"popper.js": "^1.16.0",
"shortid": "^2.2.8",
"slugify": "^1.3.6",
"vue": "^2.6.10",
"vue-router": "^3.1.3",
"vuex": "^3.1.2"
},
"devDependencies": {
"@vue/cli-plugin-babel": "^4.1.0",
"@vue/cli-plugin-e2e-cypress": "^4.1.0",
"@vue/cli-plugin-router": "^4.1.0",
"@vue/cli-plugin-unit-jest": "^4.1.0",
"@vue/cli-plugin-vuex": "^4.1.0",
"@vue/cli-service": "^4.1.0",
"@vue/test-utils": "1.0.0-beta.29",
"concurrently": "^5.0.2",
"graphql-tag": "^2.9.0",
"node-sass": "^4.12.0",
"sass-loader": "^8.0.0",
"vue-template-compiler": "^2.6.10"
},
"browserslist": [
"> 1%",
"last 2 versions"
],
"jest": {
"preset": "@vue/cli-plugin-unit-jest"
}
}
我只使用CSS而不是Sass解决了这个问题。 似乎是一个逃避,但我已经花太多时间在这上面了。
<template>
<div>
<span class="oi" :title="icon" aria-hidden="true" :class="`oi-${icon}`"></span>
</div>
</template>
<script>
export default {
name: "iconic-icon",
props: {
icon: String
}
};
</script>
<style scoped lang="scss">
@import "~open-iconic/font/css/open-iconic-bootstrap.css";
</style>
此类问题可能与 SaaS 加载程序的工作方式有关。 我在我的一个项目中遇到了同样的问题,并通过将字体目录移动到 assets 文件夹来解决它。
如果你谷歌这个,你会发现其他人找到了与我在这里发布的相同的解决方案。
在您的情况下,您应该将 $icon-font-path 变量更改为以下内容: $icon-font-path: '/assets/fonts/' !default;
它会起作用!