angular-docs-cn/aio/content/guide/setup.md

370 lines
8.7 KiB
Markdown
Raw Normal View History

@title
Setup for local development
@intro
Install the Angular QuickStart seed for faster, more efficient development on your machine.
@description
{@a develop-locally}
2017-03-31 19:57:13 -04:00
## Setup a local development environment
The <live-example name=quickstart>QuickStart live-coding</live-example> example is an Angular _playground_.
It's not where you'd develop a real application.
You [should develop locally](guide/setup#why-locally "Why develop locally") on your own machine ... and that's also how we think you should learn Angular.
Setting up a new project on your machine is quick and easy with the **QuickStart seed**,
maintained [on github](https://github.com/angular/quickstart "Install the github QuickStart repo").
2017-03-31 19:57:13 -04:00
Make sure you have [node and npm installed](guide/setup#install-prerequisites "What if you don't have node and npm?").
Then ...
2017-03-31 19:57:13 -04:00
1. Create a project folder (you can call it `quickstart` and rename it later).
1. [Clone](guide/setup#clone "Clone it from github") or [download](guide/setup#download "download it from github") the **QuickStart seed** into your project folder.
1. Install [npm](guide/setup#install-prerequisites "What if you don't have node and npm?") packages.
1. Run `npm start` to launch the sample application.
{@a clone}
2017-03-31 19:57:13 -04:00
### Clone
Perform the _clone-to-launch_ steps with these terminal commands.
<code-example language="sh" class="code-shell">
2017-03-31 19:57:13 -04:00
git clone https://github.com/angular/quickstart.git quickstart
cd quickstart
npm install
npm start
</code-example>
2017-04-10 11:51:13 -04:00
<div class="alert is-important">
2017-03-31 19:57:13 -04:00
`npm start` fails in _Bash for Windows_ which does not support networking to servers as of January, 2017.
2017-04-10 11:51:13 -04:00
</div>
{@a download}
2017-03-31 19:57:13 -04:00
### Download
<a href="https://github.com/angular/quickstart/archive/master.zip" title="Download the QuickStart seed repository">Download the QuickStart seed</a>
and unzip it into your project folder. Then perform the remaining steps with these terminal commands.
<code-example language="sh" class="code-shell">
2017-03-31 19:57:13 -04:00
cd quickstart
npm install
npm start
</code-example>
2017-04-10 11:51:13 -04:00
<div class="alert is-important">
2017-03-31 19:57:13 -04:00
`npm start` fails in _Bash for Windows_ which does not support networking to servers as of January, 2017.
2017-04-10 11:51:13 -04:00
</div>
{@a non-essential}
2017-03-31 19:57:13 -04:00
## Delete _non-essential_ files (optional)
You can quickly delete the _non-essential_ files that concern testing and QuickStart repository maintenance
(***including all git-related artifacts*** such as the `.git` folder and `.gitignore`!).
2017-04-10 11:51:13 -04:00
<div class="alert is-important">
2017-03-31 19:57:13 -04:00
Do this only in the beginning to avoid accidentally deleting your own tests and git setup!
2017-04-10 11:51:13 -04:00
</div>
2017-03-31 19:57:13 -04:00
Open a terminal window in the project folder and enter the following commands for your environment:
### OS/X (bash)
<code-example language="sh" class="code-shell">
2017-03-31 19:57:13 -04:00
xargs rm -rf &lt; non-essential-files.osx.txt
rm src/app/*.spec*.ts
rm non-essential-files.osx.txt
</code-example>
2017-03-31 19:57:13 -04:00
### Windows
<code-example language="sh" class="code-shell">
2017-03-31 19:57:13 -04:00
for /f %i in (non-essential-files.txt) do del %i /F /S /Q
rd .git /s /q
rd e2e /s /q
</code-example>
{@a seed}
2017-03-31 19:57:13 -04:00
## What's in the QuickStart seed?
2017-03-31 19:57:13 -04:00
The **QuickStart seed** contains the same application as the QuickStart playground.
But its true purpose is to provide a solid foundation for _local_ development.
Consequently, there are _many more files_ in the project folder on your machine,
2017-03-31 19:57:13 -04:00
most of which you can [learn about later](guide/setup-systemjs-anatomy "Setup Anatomy").
{@a app-files}
2017-03-31 19:57:13 -04:00
Focus on the following three TypeScript (`.ts`) files in the **`/src`** folder.
<div class='filetree'>
<div class='file'>
src
</div>
<div class='children'>
<div class='file'>
app
</div>
<div class='children'>
<div class='file'>
app.component.ts
</div>
<div class='file'>
app.module.ts
</div>
</div>
<div class='file'>
main.ts
</div>
</div>
</div>
<code-tabs>
<code-pane title="src/app/app.component.ts" path="setup/src/app/app.component.ts">
</code-pane>
<code-pane title="src/app/app.module.ts" path="setup/src/app/app.module.ts">
</code-pane>
<code-pane title="src/main.ts" path="setup/src/main.ts">
</code-pane>
</code-tabs>
2017-03-31 19:57:13 -04:00
All guides and cookbooks have _at least these core files_.
Each file has a distinct purpose and evolves independently as the application grows.
Files outside `src/` concern building, deploying, and testing your app.
They include configuration files and external dependencies.
Files inside `src/` "belong" to your app.
Add new Typescript, HTML and CSS files inside the `src/` directory, most of them inside `src/app`,
unless told to do otherwise.
The following are all in `src/`
<style>
td, th {vertical-align: top}
</style>
<table width="100%">
<col width="20%">
</col>
<col width="80%">
</col>
<tr>
<th>
File
</th>
<th>
Purpose
</th>
</tr>
<tr>
<td>
<code>app/app.component.ts</code>
</td>
<td>
2017-03-31 19:57:13 -04:00
Defines the same `AppComponent` as the one in the QuickStart playground.
It is the **root** component of what will become a tree of nested components
as the application evolves.
</td>
</tr>
<tr>
<td>
<code>app/app.module.ts</code>
</td>
<td>
2017-03-31 19:57:13 -04:00
Defines `AppModule`, the [root module](guide/appmodule "AppModule: the root module") that tells Angular how to assemble the application.
Right now it declares only the `AppComponent`.
Soon there will be more components to declare.
</td>
</tr>
<tr>
<td>
<code>main.ts</code>
</td>
<td>
2017-03-31 19:57:13 -04:00
Compiles the application with the [JIT compiler](glossary#jit) and
[bootstraps](guide/appmodule#main "bootstrap the application")
the application's main module (`AppModule`) to run in the browser.
The JIT compiler is a reasonable choice during the development of most projects and
it's the only viable choice for a sample running in a _live-coding_ environment like Plunker.
You'll learn about alternative compiling and [deployment](guide/deployment) options later in the documentation.
</td>
</tr>
</table>
2017-04-10 11:51:13 -04:00
<div class="l-sub-section">
2017-03-31 19:57:13 -04:00
### Next Step
2017-03-31 19:57:13 -04:00
If you're new to Angular, we recommend staying on the [learning path](guide/learning-angular "Angular learning path").
2017-04-10 11:51:13 -04:00
</div>
<br></br><br></br>
{@a install-prerequisites}
2017-03-31 19:57:13 -04:00
## Appendix: node and npm
2017-03-31 19:57:13 -04:00
Node.js and npm are essential to modern web development with Angular and other platforms.
Node powers client development and build tools.
The _npm_ package manager, itself a _node_ application, installs JavaScript libraries.
<a href="https://docs.npmjs.com/getting-started/installing-node" target="_blank" title="Installing Node.js and updating npm">
Get them now</a> if they're not already installed on your machine.
**Verify that you are running node `v4.x.x` or higher and npm `3.x.x` or higher**
by running the commands `node -v` and `npm -v` in a terminal/console window.
Older versions produce errors.
We recommend [nvm](https://github.com/creationix/nvm) for managing multiple versions of node and npm.
You may need [nvm](https://github.com/creationix/nvm) if you already have projects running on your machine that
use other versions of node and npm.
{@a why-locally}
2017-03-31 19:57:13 -04:00
## Appendix: Why develop locally
<live-example title="QuickStart Seed in Plunker">Live coding</live-example> in the browser is a great way to explore Angular.
Links on almost every documentation page open completed samples in the browser.
You can play with the sample code, share your changes with friends, and download and run the code on your own machine.
2017-03-31 19:57:13 -04:00
The [QuickStart](quickstart "Angular QuickStart Playground") shows just the `AppComponent` file.
It creates the equivalent of `app.module.ts` and `main.ts` internally _for the playground only_.
so the reader can discover Angular without distraction.
The other samples are based on the QuickStart seed.
As much fun as this is ...
2017-03-31 19:57:13 -04:00
* you can't ship your app in plunker
* you aren't always online when writing code
* transpiling TypeScript in the browser is slow
* the type support, refactoring, and code completion only work in your local IDE
2017-03-31 19:57:13 -04:00
Use the <live-example title="QuickStart Seed in Plunker">live coding</live-example> environment as a _playground_,
a place to try the documentation samples and experiment on your own.
It's the perfect place to reproduce a bug when you want to
<a href="https://github.com/angular/angular.io/issues/new" target="_blank" title="File a documentation issue">file a documentation issue</a> or
<a href="https://github.com/angular/angular/issues/new" target="_blank" title="File an Angular issue">file an issue with Angular itself</a>.
For real development, we strongly recommend [developing locally](guide/setup#develop-locally).