【问题标题】:require bootstrap and jquery in aurelia app在 aurelia 应用程序中需要 bootstrap 和 jquery
【发布时间】:2016-10-23 12:09:15
【问题描述】:

我是 Aurelia 的新手,所以我不太确定这应该如何工作。我创建了一个新的 Aurelia 项目并安装了bootstrap,只需执行jspm install bootstrap。我在控制台中看到这也引入了jquery 3.0.0

现在我的问题是,如何在我的项目中使用bootstrap.cssbootstrap.jsjquery.js

第一次尝试:

app.html 中,我尝试执行以下操作:

<require from="bootstrap"></require>

我试过了,因为我的 config.js 中有以下行:

map: {
    ...
    "bootstrap": "github:twbs/bootstrap@3.3.6",
    ...
}

这种方法在加载bootstrap.js 的意义上是有效的,但随后在浏览器中出现错误,提示它缺少jquery.js。所以它不会自动为我加载 jquery。 这正常吗?

第二次尝试:

我在app.html 中将我的require 更改为这个:

<require from="jquery/dist/jquery.js"></require>
<require from="bootstrap/css/bootstrap.css"></require>
<require from="bootstrap/js/bootstrap.js"></require>

我不确定它如何知道在哪里查找 bootstrap.jsbootstrap.css 文件,因为它们位于:jspm_packages/github/twbs/bootstrap@3.3.6/css/bootstrap.css 等。但它知道如何找到引导文件。但不是 jquery 文件。

我在我的 config.js 中有这个用于 jquery:

map: {
    ...
    "github:twbs/bootstrap@3.3.6": {
      "jquery": "npm:jquery@3.0.0"
    },
    ....
}

所以基本上我的问题是,这应该如何工作?当我&lt;require from="bootstrap"&gt; 时,require 是否应该自动加载所有必要的文件。还是应该将它们作为单独的文件加载?如果是这样,在这种情况下我该如何加载 jquery?

【问题讨论】:

    标签: aurelia jspm


    【解决方案1】:

    require 元素用于拉入 Aurelia 组件、html 模板(即 Aurelia 组件)或 css 文件。它不是用来加载 javascript 文件的。

    Aurelia 骨架展示了如何在其main.js 文件中加载 Bootstrap:

    import 'bootstrap';
    

    是文件的第一行。这将初始化 Bootstrap 的 javascript 代码。

    app.html 中一个require 元素用于加载Bootstrap 的css:

    <require from="bootstrap/css/bootstrap.css"></require>
    

    将 jQuery 导入文件也非常简单:

    import $ from 'jquery';
    

    然后您可以随意使用$ 函数。

    【讨论】:

    • 像你说的那样导入时找不到jquery。尽管有一个 jquery 文件位于:jspm_packages/npm/jquery@3.0.0/dist/jquery.js.
    • 您可能运行了jspm install bootstrap,但您还需要运行jspm install jquery 才能导入jquery,就像我在上面所做的那样。
    • 刚刚跑了jspm install jquery。现在它可以加载jquery.js。但是,它仍然抱怨bootstrap 需要jQuery 插件。我首先有import $ from 'jquery',然后是import 'bootstrap。那么这可能是一个错误还是我仍然错过了什么?
    • 我建议将您的代码与位于此处的 aurelia esnext 骨架进行比较:github.com/aurelia/skeleton-navigation/tree/master/…
    • import 'bootstrap'; import 'jquery'; in app.js 为我工作!使用 aurelia cli 构建的项目。在“aurelia.json”中添加了 jquery 和 bootstrap 的条目,如 contact manager tutorial 中所述
    【解决方案2】:

    我遇到了这个问题,然后安装了最新的节点和 npm,然后从 aurelia 网站上的教程页面:-

    要设置 Bootstrap,我们首先使用 NPM 安装库本身。在命令行上执行以下命令:

    npm install bootstrap --save
    

    接下来,因为 Bootstrap 使用了 jQuery,我们也想安装 jQuery,像这样:

    npm install jquery@^2.2.4 --save
    

    然后在更新包时重新启动应用程序并再次运行它...已修复!

    【讨论】:

      【解决方案3】:

      添加后 导入“引导”; 在 main.js 中,您可能需要停止应用程序 (Ctrl + c) 并再次运行它 au run --watch 让它发挥作用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-04
        • 1970-01-01
        • 1970-01-01
        • 2012-07-17
        • 2019-07-01
        • 2020-11-21
        • 2018-07-13
        相关资源
        最近更新 更多