jquery - Bower:jquery 被声明为依赖项,但不知何故丢失

标签 jquery gruntjs yeoman bower

我有一个带有这些依赖项的 Bower.json:

"dependencies": {
    "bootstrap": "~3.0.2",
    "jquery": "~2.0.3",
    "modernizr": "~2.7.0"
  }

然后使用 grunt 构建生产文件。

Bower 和 Grunt 是使用 yeoman 和生成器 webapp 配置的(之后我添加了 bootstrap3 并更新了 jquery 和 Modernizr)。

页面显示此错误:“未捕获错误:Bootstrap 需要 jQuery”

可能出了什么问题?

有没有办法确保 jquery 在引导之前加载?我尝试更改 Bower.json 中依赖项的顺序,但没有帮助。

难道构建过程中没有包含jquery?我怎么会知道? (我是一名自耕农、弓箭手和咕噜菜鸟)

这是我的index.html:

<!doctype html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="CH"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8" lang="CH"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9" lang="CH"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="CH"> <!--<![endif]-->
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>gabriel</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
        <!-- build:css(.tmp) styles/main.css -->
        <!-- bower:css -->
        <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css" />
        <!-- endbower -->
        <!-- endbuild -->
        <!-- build:js scripts/vendor/modernizr.js -->
        <script src="bower_components/modernizr/modernizr.js"></script>
        <!-- endbuild -->
    </head>
    <body>
        <!--[if lt IE 10]>
            <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
        <![endif]-->


        <div class="hero-unit">
          <h1>'Allo, 'Allo!</h1>
          <p>You now have</p>
          <ul>
            <li>HTML5 Boilerplate</li>
            <li>Modernizr</li>
          </ul>
        </div>


        <!-- build:js scripts/vendor.js -->
        <!-- bower:js -->
        <script src="bower_components/bootstrap/dist/js/bootstrap.js"></script>
        <!-- endbower -->
        <!-- endbuild -->

        <!-- Google Analytics: change UA-XXXXX-X to be your site's ID. -->
        <script>
            (function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
            function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
            e=o.createElement(i);r=o.getElementsByTagName(i)[0];
            e.src='//www.google-analytics.com/analytics.js';
            r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
            ga('create','UA-XXXXX-X');ga('send','pageview');
        </script>

        <!-- build:js scripts/main.js -->
        <script src="scripts/main.js"></script>
        <!-- endbuild -->
</body>
</html>

最佳答案

使用 Bower 获取库有 2 个步骤:

  1. 下载它们
    首先检查 bower_components 中是否存在 jQuery。 .
    每当手动编辑bower.json ,您还必须运行 bower update将库下载到 bower_components .
    运行bower update如果您使用 bower install 引入了新的依赖项,则不需要.

  2. 将它们注入(inject)您的应用代码中 ( index.html )
    Here's how to do it with grunt-bower-install 。另一种可能性是使用 RequireJS。

    如果 Bower 在这一步有任何问题,请随时添加必要的 <script ... </script>给您index.html手动。通常,当我添加新的依赖项时,在随后的 grunt bower-install 调用中第一次调用将从 index.html 中删除所有先前的依赖项并只留下新的依赖项,并且其后的调用将注入(inject)除新依赖项之外的所有依赖项。

关于jquery - Bower:jquery 被声明为依赖项,但不知何故丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20134015/

相关文章:

jquery - 动态加载jquery数据表

css - 使用 Grunt 处理依赖文件并重写路径

javascript - 第一次运行 Grunt build 时出错

javascript - 由于空闲超时插件,页面加载事件多次触发?

JavaScript 日期到时间戳计算不适用于任何一个月的 8 号和 9 号

javascript - 通过按钮动态传递值

gruntjs - Grunt Livereload 和 MAMP

javascript - yeoman 子生成器如何访问主生成器上定义的变量?

node.js - 安装 yeoman 生成器后是否可以运行 grunt 命令?

node.js - nodejs/grunt usemin 插件