javascript - 即使文件路径正确,也无法从 bower_components 加载资源

标签 javascript html css browser-sync

我在处理 Web 元素时遇到了一些令人沮丧的 404 错误。我正在为我的 CSS 和 JS 文件使用这个文件结构:

bower_components
    bootstrap
        |dist
            |css
               | bootstrap.min.css
    angular
        |angular.min.js
    jquery
        |dist
            |jquery.min.js
dist
lib
    |farbtastic.js
node_modules
src
    |img
    |styles
    |scripts
    |view1
    |view2
    |index.html //running browserSync from this folder using this file as index

当我尝试引用 index.html 中的文件时使用适当的语法:

<link rel="stylesheet" type="text/css" href="../bower_components/bootstrap/dist/css/bootstrap.min.css">

我收到 404“无法加载资源”错误。

../语法似乎是正确的,我的控制台显示它正在 http://localhost:3000/bower_components/bootstrap/dist/css/bootstrap.min.css 中的正确位置查找.

我有没有做错什么?

最佳答案

您的应用程序的根目录是“src”文件夹。当您从“../bower_components/”请求 bootstrap.min.css 时,您是在告诉浏览器向上一级。因为您已经位于应用程序的根目录,所以“../bower_component”本质上与“bower_component”的作用相同。

您可以:

1. move your index.html 1 level up
2. move your bower_components inside src
3. have some kind of build(gulp/grunt) to reorganize them in a way where the bower_components is within the root folder.

关于javascript - 即使文件路径正确,也无法从 bower_components 加载资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34531928/

相关文章:

javascript - jQuery 中的弹性文本区域插件

javascript - "+"js中的字符回显来自html页面的编辑数据被替换为数据库中的空白

css - 100% 宽度的容器

javascript - 使用 Bootstrap 添加灯箱

css - Codeigniter 基本 url 在 header 中不起作用

javascript - 如何更改 DOM 元素?

javascript - 响应我的命令的倒数计时器

javascript - 动态图像更新在 mozilla 中不起作用

jquery - 通过第 n 个父 Div 禁用子 Div

javascript - 将选定的记录加载到模态窗口