简体   繁体   English

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

[英]Failed to load resource from bower_components even with correct file path

I'm running into some frustrating 404 errors while working on a web project. 在处理Web项目时,我遇到了一些令人沮丧的404错误。 I'm using this file structure for my CSS and JS files: 我将这个文件结构用于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

When I try to reference files in index.html using the appropriate syntax: 当我尝试使用适当的语法在index.html引用文件时:

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

I get a 404 "Failed to load resource" error. 我收到404“无法加载资源”错误。

The ../ syntax appears to be correct, and my console shows that it's looking in the correct location in http://localhost:3000/bower_components/bootstrap/dist/css/bootstrap.min.css . ../语法似乎是正确的,并且我的控制台显示它在http://localhost:3000/bower_components/bootstrap/dist/css/bootstrap.min.css中的正确位置中查找。

Is there something I'm not doing correctly? 有什么我做不正确的事情吗?

The root directory of your application is the "src" folder. 应用程序的根目录是“ src”文件夹。 When you request bootstrap.min.css from "../bower_components/", you are telling the browser to go 1 level up. 当您从“ ../bower_components/”请求bootstrap.min.css时,您是在告诉浏览器提高1级。 Because you are already at the root of the application, "../bower_component" is essentially doing the same as "bower_component". 因为您已经在应用程序的根目录中,所以“ ../bower_component”在本质上与“ bower_component”相同。

You can: 您可以:

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.

Since root directory of your application is src folder, you can still load from bower_components using the link 由于应用程序的根目录是src文件夹,因此您仍然可以使用链接从bower_components加载

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

configure you browserSync.init this way browserSync.init这种方式配置browserSync.init

browserSync.init({
        server: {
                baseDir :"src",
                routes:{
                    "/bower_components" : "bower_components"
                }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM