从 NUXT 布局中的“assets”目录加载 css 文件

xx *_* yy 0 node.js vue.js vuejs2 nuxt.js

在nuxt布局(default.vue)中,我想从assets文件夹加载图像和css文件,图像加载成功,但css文件没有,为什么?

/layouts/default.vue

<template>
<img src="~assets/photo.jpg" />
 <!-- converted to /_nuxt/assets/photo.jpg and loaded successfully -->
</template>

<script>
export default{
 head:{
  link: [  { rel: 'stylesheet', href: '~assets/style.css' }]
}
}
</sript>
Run Code Online (Sandbox Code Playgroud)

当我查看源代码时:

<link href="~assets/style.css" />
Run Code Online (Sandbox Code Playgroud)

并且加载失败

也导航到http://localhost:3000/_nuxt/assets/style.css 失败,但http://localhost:3000/_nuxt/assets/photo.jpg成功

笔记:

我不想将 style.css 放在 'static' 文件夹中,因为我想通过 webpack css-loader 加载它以添加缓存哈希

Psi*_*dom 6

图片 src 是 Vue 自动编译的,更多可以在相对路径导入查看;从文档:

所有资产的URL,例如<img src="...">background: url(...)和CSS @import的解析为模块依赖关系。

对于上面列出的情况之外的自定义路径,您需要明确要求将其编译为静态资产路径的路径:

export default{
  head:{
    link: [{ rel: 'stylesheet', href: require('~assets/style.css') }]
  }
}
Run Code Online (Sandbox Code Playgroud)