2019-05-06 07:31:26 -04:00
|
|
|
|
---
|
|
|
|
|
id: 587d7fb5367417b2b2512c03
|
2021-02-16 13:01:20 -08:00
|
|
|
|
title: 用脱字符(^)来使用依赖项的最新次要版本
|
2019-05-06 07:31:26 -04:00
|
|
|
|
challengeType: 2
|
2020-09-18 00:12:32 +08:00
|
|
|
|
forumTopicId: 301531
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: use-the-caret-character-to-use-the-latest-minor-version-of-a-dependency
|
2019-05-06 07:31:26 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-02-16 13:01:20 -08:00
|
|
|
|
和上一个挑战中我们学到的用波浪号来安装最新的修订版依赖一样,脱字符(`^`)也允许 npm 来安装功能更新。 它们的不同之处在于:脱字符允许次版本和修订版更新。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-02-16 13:01:20 -08:00
|
|
|
|
现在项目中的 moment 依赖包的版本应该是“~2.10.2”,这意味着 npm 可以安装最新的 2.10.x 版的 moment, 如果使用脱字符(^)来替换版本号的前缀,那么 npm 可以将 moment 升级安装到任何 2.x.x 的版本。
|
2020-09-18 00:12:32 +08:00
|
|
|
|
|
|
|
|
|
```json
|
|
|
|
|
"package": "^1.3.8"
|
|
|
|
|
```
|
|
|
|
|
|
2021-02-16 13:01:20 -08:00
|
|
|
|
这会将依赖包更新到任意的 1.x.x 版本。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
# --instructions--
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
2021-02-16 13:01:20 -08:00
|
|
|
|
在依赖项中,使用脱字符(`^`)为 moment 的版本添加前缀,允许 npm 更新依赖包到任意新的次版本。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
|
**注意:** 原来的版本号不用更改。
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
2021-02-16 13:01:20 -08:00
|
|
|
|
“dependencies”字段中应包含“moment”
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
(getUserInput) =>
|
|
|
|
|
$.get(getUserInput('url') + '/_api/package.json').then(
|
|
|
|
|
(data) => {
|
|
|
|
|
var packJson = JSON.parse(data);
|
|
|
|
|
assert.property(
|
|
|
|
|
packJson.dependencies,
|
|
|
|
|
'moment',
|
|
|
|
|
'"dependencies" does not include "moment"'
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
(xhr) => {
|
|
|
|
|
throw new Error(xhr.responseText);
|
|
|
|
|
}
|
|
|
|
|
);
|
2019-05-06 07:31:26 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-02-16 13:01:20 -08:00
|
|
|
|
“moment”的版本应是“^2.x.x”
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
(getUserInput) =>
|
|
|
|
|
$.get(getUserInput('url') + '/_api/package.json').then(
|
|
|
|
|
(data) => {
|
|
|
|
|
var packJson = JSON.parse(data);
|
|
|
|
|
assert.match(
|
|
|
|
|
packJson.dependencies.moment,
|
|
|
|
|
/^\^2\./,
|
|
|
|
|
'Wrong version of "moment". It should be ^2.10.2'
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
(xhr) => {
|
|
|
|
|
throw new Error(xhr.responseText);
|
|
|
|
|
}
|
|
|
|
|
);
|
2019-05-06 07:31:26 -04:00
|
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
2020-09-18 00:12:32 +08:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```js
|
|
|
|
|
/**
|
|
|
|
|
Backend challenges don't need solutions,
|
|
|
|
|
because they would need to be tested against a full working project.
|
|
|
|
|
Please check our contributing guidelines to learn more.
|
|
|
|
|
*/
|
|
|
|
|
```
|