javascript - Resolving image paths in external SCSS files in Nuxt.js - Stack Overflow

I am attempting to keep certain ponent scss files sepeated from their Vue ponents. At the same time I a

I am attempting to keep certain ponent scss files sepeated from their Vue ponents. At the same time I am also including a GLOBAL scss file which will not be scoped. Regardless of which files I use, I can not get the /assets or /static image paths to resolve appropriately.

A sample of my nuxt.config.js:

module.exports = {
   css: [
      // SCSS file in the project
      '@/assets/scss/base.scss'
   ],
}

In my individual view file I am importing the ponent scss file this way:

<style lang="scss">
   @import "../assets/scss/pages/home";
</style>

Regardless of which way I load, I cannot resolve the following paths in scss:

.myClass {
  background-image: url('~assets/img/my-image.jpg');
}

OR

.myClass {
  background-image: url('~static/img/my-image.jpg');
}

OR

.myClass {
  background-image: url('/img/my-image.jpg');
}

All of these end with 404s. I'm wracking my brain on this one. Assets are in both /static and /assets for the sake of testing.

Any ideas on what I could be doing wrong?

I am attempting to keep certain ponent scss files sepeated from their Vue ponents. At the same time I am also including a GLOBAL scss file which will not be scoped. Regardless of which files I use, I can not get the /assets or /static image paths to resolve appropriately.

A sample of my nuxt.config.js:

module.exports = {
   css: [
      // SCSS file in the project
      '@/assets/scss/base.scss'
   ],
}

In my individual view file I am importing the ponent scss file this way:

<style lang="scss">
   @import "../assets/scss/pages/home";
</style>

Regardless of which way I load, I cannot resolve the following paths in scss:

.myClass {
  background-image: url('~assets/img/my-image.jpg');
}

OR

.myClass {
  background-image: url('~static/img/my-image.jpg');
}

OR

.myClass {
  background-image: url('/img/my-image.jpg');
}

All of these end with 404s. I'm wracking my brain on this one. Assets are in both /static and /assets for the sake of testing.

Any ideas on what I could be doing wrong?

Share Improve this question asked Dec 8, 2017 at 21:02 radiantstaticradiantstatic 3424 silver badges20 bronze badges 4
  • I think this will definitely help :stackoverflow./a/940475/1971378. Its relative to the CSS file & ! the document. – trk Commented Dec 8, 2017 at 22:35
  • @82Tuskers - yes, I’m aware of how relative paths are handled in CSS. This is a Nuxt / Webpack related issue where regardless of where I’ve been placing the images, using the example code directly from Nuxt, I am not getting my assets served appropriarelt. The ~ is a symbol handled by the build process and traditionally all of the css is inlined in the head. – radiantstatic Commented Dec 9, 2017 at 23:40
  • try ~/assets/my-image.jpg? – Hammerbot Commented Dec 10, 2017 at 0:20
  • try background: url("./assets/my-image.jpg"); – Syed Commented Jun 19, 2018 at 10:29
Add a ment  | 

2 Answers 2

Reset to default 5

This works for me in assets case:

.myClass {
  background-image: url('~@/assets/img/my-image.jpg');
}

While I could not resolve the issue within the CSS, I was able to get it to work by setting the style property directly on the element

:style="{'background-image': `url(${backgroundLocation})`}

In my case I imported the image and passed it to the template within data. This meant that backgroundLocation resolved to something like /_nuxt/img/..... Then you need to prepend the url() css function.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744347593a4569779.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信