为什么我们要为 Angular 2.0 安装 Node.js?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37267510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do we have install Node.js for Angular 2.0?
提问by user2572003
I started a tutorial on Angular 2.0, the first step in setting up the workspace is installing Node.js and NPM.
我开始了一个关于 Angular 2.0 的教程,设置工作区的第一步是安装 Node.js 和 NPM。
Why do we have install Node.js for Angular 2.0?
为什么我们要为 Angular 2.0 安装 Node.js?
I don't remember doing this for angular 1.X.
我不记得为 angular 1.X 做这件事。
采纳答案by Jeroen
Technically, Node.js and NPM are not needed to do Angular2 work. It does ease things though. Here's the main reasons I speculate are behind this choice:
从技术上讲,做 Angular2 的工作不需要 Node.js 和 NPM。不过它确实让事情变得轻松。以下是我推测做出这一选择的主要原因:
CLI: Since a while now the de facto way to build and develop new Angular apps is to use the CLI toolingwhich relies on Node and NPM as well.
TypeScript: Examples are .ts, and you need to run a compiler step to get them into .js, which can be done on-the-fly easily with Node.js and NPM (plus it's a way of easily getting typing files);
- Web Server: Serving your Angular SPA from a "real" albeit light web server prevents probably some nasty issues that come with checking your site using
file://links.
CLI:一段时间以来,构建和开发新 Angular 应用程序的实际方法是使用CLI 工具,它也依赖于 Node 和 NPM。
TypeScript:示例是 .ts,您需要运行一个编译器步骤将它们转换为 .js,这可以使用 Node.js 和 NPM 轻松地即时完成(而且它是一种轻松获取输入文件的方法);
- Web 服务器:从“真实”的Web 服务器为您的 Angular SPA 提供服务,尽管使用
file://链接检查您的网站可能会阻止一些令人讨厌的问题。
The Quickstart guide itself actually continues to mention some more concrete reasons as well:
快速入门指南本身实际上也继续提到一些更具体的原因:
Here's what these scripts do:
npm start- runs the compiler and a server at the same time, both in "watch mode"
npm run tsc- runs the TypeScript compiler once
npm run tsc:w- runs the TypeScript compiler in watch mode; the process keeps running, awaiting changes to TypeScript files and re-compiling when it sees them
npm run lite- runs the lite-server, a light-weight, static file server with excellent support for Angular apps that use routing
npm run typings- runs the typings tool separately
npm run postinstall- called by npm automatically after it successfully completes package installation. This script installs the TypeScript definition files defined in typings.json
以下是这些脚本的作用:
npm start- 同时运行编译器和服务器,都在“观察模式”
npm run tsc- 运行一次 TypeScript 编译器
npm run tsc:w- 在监视模式下运行 TypeScript 编译器;进程继续运行,等待对 TypeScript 文件的更改并在看到它们时重新编译
npm run lite- 运行 lite-server,这是一个轻量级的静态文件服务器,对使用路由的 Angular 应用程序提供出色的支持
npm run typings- 单独运行打字工具
npm run postinstall- 成功完成包安装后由 npm 自动调用。此脚本安装在 Typings.json 中定义的 TypeScript 定义文件
You can also have a look at the Quickstart sourceand further dive into where NPM is needed.
您还可以查看Quickstart 源代码并进一步深入了解需要 NPM 的地方。
Footnote: there's a similar question about needing Node.js for AngularJS (1.x).
回答by Peter
Because Anglar2 is based on Typescript, Web Components and ES6 which need compilation for performance and broader browser support. Typescript is compiled to ES5JavaScript and the other features require shims for backwards compatibility.
因为 Anglar2 基于 Typescript、Web Components 和 ES6,它们需要编译以获得性能和更广泛的浏览器支持。Typescript 被编译为 ES5JavaScript,其他功能需要垫片才能向后兼容。
Since Typescript is a superset of JavaScript, and it's compiled to JavaScript anyway, you can write your code in plain JavaScript but it's not recommended.
由于 Typescript 是 JavaScript 的超集,并且无论如何它都会编译为 JavaScript,因此您可以使用纯 JavaScript 编写代码,但不建议这样做。
For a more detailed explanation check out these videos on YouTube
有关更详细的解释,请查看 YouTube 上的这些视频
回答by Peter
NodeJS gives you the tool npm that allows you to download libraries and packages you would use in Angular 2. From the shell you can go to your folder and type npm install to install dependencies you need to have installed to get your angular project going. It will make it easier for you! If you want a complete starter kit go to https://github.com/buckyroberts, you can fork or download the zip with all the starter files to get you going :)
NodeJS 为您提供了 npm 工具,它允许您下载您将在 Angular 2 中使用的库和包。从 shell 中,您可以转到您的文件夹并键入 npm install 来安装您需要安装的依赖项,以使您的 Angular 项目运行。它会让你更轻松!如果你想要一个完整的入门工具包,请访问https://github.com/buckyroberts,你可以 fork 或下载包含所有入门文件的 zip 来让你开始:)
回答by Rohit Luthra
You do not need to use Node anywhere in production to use any front-end JavaScript framework, whether it is jQuery, AngularJS, ReactJS, Angular2, etc.
您不需要在生产中的任何地方使用 Node 来使用任何前端 JavaScript 框架,无论是 jQuery、AngularJS、ReactJS、Angular2 等。
Angular2 can be used in isolation but to get and feel better development environment, angular2 should be used with nodejs and npm. Some of the nodejs modules helps you in web development.
Angular2 可以单独使用,但为了获得和感受更好的开发环境,angular2 应该与 nodejs 和 npm 一起使用。一些 nodejs 模块可以帮助您进行 Web 开发。

