网站首页 » 前端开发 » Angular 2 » Angular 2+ 执行 ng eject 后,我要回到过去
上一篇:
下一篇:

Angular 2+ 执行 ng eject 后,我要回到过去

前言

ng eject 是什么?它的作用就是弹出 webpack.config 配置文件以及修改了 package.json 文件和 .angular-cli.json 文件里的一些默认配置。

在这里我们不关注 ng eject 的过多细节,因为本文要分享的是如何才能让我回到过去(ng eject 执行之前)。因为执行了 ng eject 命令后,之前的 ng 前端的命令行就没法使用了。所以你懂的……

回顾

首先我们来看看 ng eject 命令执行后,我们再执行 ng (如:ng serve –open)命令会提示什么

我的天

Your global Angular CLI version (1.5.4) is greater than your local version (1.2.0). The local Angular CLI version is used.

To disable this warning use “ng set –global warnings.versionMismatch=false”.

An ejected project cannot use the build command anymore.

 

上面我们已经知道了在执行 ng eject 命令后就会修改 package.json 和 弹出了一个 webpack.config 配置文件,于是我们就可以对症下药了。

就是干

来到了就是干环节。

步骤 1

这里有一个最简单的方法,那么就是直接修改 .angular-cli.json 文件

部分代码
"project": {
    "name": "contacts",
    "ejected": false
  },

把 project 对象里的 ejected 属性设置为false。这样做当然是最快的,但其它文件已经修改了的代码和多出来的代码怎么办?所以为了让文件变得更精简我们有必需把没用的代码删掉。

为此,我们先把 project 对象里的 ejected 删掉。接下来恢复和删掉 package.json 文件里的代码。

步骤 2

ng eject 执行前 package.json 文件是这样的:

原来的代码
{
  "name": "contacts",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
},
  "private": true,
  "dependencies": {
    "@angular/animations": "^4.0.0",
    "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/forms": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "core-js": "^2.4.1",
    "rxjs": "^5.1.0",
    "zone.js": "^0.8.4"
  },
  "devDependencies": {
    "@angular/cli": "1.2.0",
    "@angular/compiler-cli": "^4.0.0",
    "@angular/language-service": "^4.0.0",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "~3.0.1",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.0.4",
    "tslint": "~5.3.2",
    "typescript": "~2.3.3"
  }
}

ng eject 命令执行后是变成了这样

后来的代码
{
  "name": "contacts",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "webpack-dev-server --port=4200",
    "build": "webpack",
    "test": "karma start ./karma.conf.js",
    "lint": "ng lint",
    "e2e": "protractor ./protractor.conf.js",
    "pree2e": "webdriver-manager update --standalone false --gecko false --quiet"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^4.0.0",
    "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/forms": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "core-js": "^2.4.1",
    "rxjs": "^5.1.0",
    "zone.js": "^0.8.4"
  },
  "devDependencies": {
    "@angular/cli": "1.2.0",
    "@angular/compiler-cli": "^4.0.0",
    "@angular/language-service": "^4.0.0",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "~3.0.1",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.0.4",
    "tslint": "~5.3.2",
    "typescript": "~2.3.3",

    / 下面是多出来的配置项 /
    "webpack-dev-server": "~2.4.5",
    "webpack": "~2.4.0",
    "autoprefixer": "^6.5.3",
    "css-loader": "^0.28.1",
    "cssnano": "^3.10.0",
    "exports-loader": "^0.6.3",
    "file-loader": "^0.10.0",
    "json-loader": "^0.5.4",
    "less-loader": "^4.0.2",
    "postcss-loader": "^1.3.3",
    "postcss-url": "^5.1.2",
    "raw-loader": "^0.5.1",
    "sass-loader": "^6.0.3",
    "script-loader": "^0.7.0",
    "source-map-loader": "^0.2.0",
    "istanbul-instrumenter-loader": "^2.0.0",
    "style-loader": "^0.13.1",
    "stylus-loader": "^3.0.1",
    "url-loader": "^0.5.7"
  }
}

有点买家秀卖家秀的感觉,不难看出,ng eject 命令执行后修改了 scripts 的配置项以及 devDependencies 的配置项,所以把这些修改了的代码恢复回去就好。

步骤 3

最后一步就是在项目中找到 webpack.config 文件,直接删除掉。

这样就完美回到过去了,angular 2+ 执行 ng eject 后,我要回到过去的故事就分享到这里。

  • 微信扫一扫,赏我

  • 支付宝扫一扫,赏我

声明

原创文章,不经本站同意,不得以任何形式转载,如有不便,请多多包涵!

本文永久链接:http://yunkus.com/angular-undo-ng-eject/

Leave a Reply

Your email address will not be published. Required fields are marked *

评论 END