Bower: 安装2个版本的 jQuery

如何安装2个版本的 jQuery使用鲍尔?我希望使用 v2.0和1.9.1作为浏览器支持回退

我遇到的问题是,如果运行 bower install jquery#1.9.1 jquery#2.0.0,第一个版本会被第二个版本覆盖,因为它们是同一个组件

57411 次浏览

In the dependencies part of your bower.json you can have something like this:

"dependencies": {
"jquery": "2.0.0",
"jquery-1.9.1": "http://code.jquery.com/jquery-1.9.1.js"
}

One shouldn't normally have to do this, but sometimes you have to maintain / migrate an existing website that (for whatever reason) uses different versions of jquery in different pages!

According to the bower docs

Bower offers several ways to install packages:

# Using the dependencies listed in the current directory's bower.json
bower install
# Using a local or remote package
bower install <package>
# Using a specific version of a package
bower install <package>#<version>
# Using a different name and a specific version of a package
bower install <name>=<package>#<version>

You can install two different versions of jQuery like so:

bower install jquery-legacy=jquery#1.10 jquery-modern=jquery#2

Or, if you prefer to set that up in a bower.json

"dependencies": {
"jquery-legacy": "jquery#1.10",
"jquery-modern": "jquery#2"
}

From the command line, if you just want the latest 1.x and 2.x versions, you can loosen the constraints in the answer above.

So:

bower install jquery-legacy=jquery#1.10 jquery-modern=jquery#2

would become:

bower install jquery-legacy=jquery#^1 jquery-modern=jquery

bower.json:

This is how i did it...

"dependencies": {
    ...
    "jquery": "2.0.0",
    "jquery-old": "1.9.1"
    ...
}

Second version, can be any version, old or new. You just have to add a different key. Like jquery-old

Install

bower install --save jquery-old

Use

Now you can use either one of the jquery version:

<script type="text/javascript" src="path/to/bower/directory/jquery/dist/jquery.min.js"></script>

<script type="text/javascript" src="path/to/bower/directory/jquery-old/dist/jquery.min.js"></script>

Bonus

"dependencies": {
    ...
    "jquery": "2.0.0",
    "jquery-old": "1.9.1"
    "jquery-latest": "^3.3.1"
    ...
}