AngularJS学习之二:配置本地开发环境
发布日期:2021-06-29 13:10:19 浏览次数:4 分类:技术文章

本文共 5960 字,大约阅读时间需要 19 分钟。

安装Angular QuickStart seed,以便在您的机器上更快,更高效地开发

设置一个本地开发环境

这个  例子是一个操场。它并不是你将要开发一个真正应用的地方。你应该在你的机器上本地化开发...... 这也是我们所认为的你应该如何去学习Angular的方法。

在你的机器上,使用这个 QuickStart seed 建立一个新的项目是快和容易的,QuickStart seed被维护在上

这个  是 QuickStart加上app.module.ts和main.ts应用文件(下面有描述),这两个文件有利于更丰富的应用例子展示。

确保你已经安装了 。 然后 ...

  1. 创建一个项目文件夹(你可以叫它 quickstart 以后再重命名)
  2.  或者  (看下面)这个 QuickStart seed 到你的项目文件夹。
  3. 安装  。
  4. 运行 npm start 去运行样例应用程序。
克隆

使用下面的这些终端命令执行以下clone-to-launch(从克隆到运行)的步骤。

git clone https://github.com/angular/quickstart.git quickstartcd quickstartnpm installnpm start

(注:
npm install类似于maven,即根据依赖配置文件package.json来下载所需要的依赖包。
参考:https://docs.npmjs.com/cli/install。)

下载

  (下载QuickStart seed)并且解压它到你的项目文件夹中。 然后用下面的这些命令执行剩余的步骤。

cd quickstartnpm installnpm start

QuickStart seed里面有什么?

这个 QuickStart seed 包含了就像QuickStart playground一样的应用程序。 但是它的真正目的是为本地开发环境提供一个坚实的基础。所以,会有 更多的文件 在你的机器的这个目录中, 其中的大部分,你稍后会学习到。

Focus on the following three TypeScript (.ts) files in the /app folder.

重点在以下三个TypeScript (.ts) 文件,位于/app文件夹中。

app
app.component.ts
app.module.ts
main.ts
import {
Component } from '@angular/core';@Component({
selector: 'my-app', template: `

Hello {
{name}}

`})export class AppComponent {
name = 'Angular'; }

所有指导和操作手册都至少有这些核心文件。每一个文件有一个不同的目的,会随着应用的发展而独立地演化。

File Purpose
app.component.ts

定义一个和在QuickStart playground中一样的 AppComponent 。它将会随着应用的演进变成一颗嵌套组件的树的根组件。

app.module.ts

定义 AppModule ,这个  将告诉 Angular如何去组装应用程序. 现在它仅仅声明了AppComponent 。很快将会有更多的组件去声明。

main.ts

使用  去编译应用程序,然后  在浏览器中去运行应用程序。 这对于大多数项目的开发来说是一个合理的选择而在类似Plunker这样的 live-coding环境运行样例代码,这是唯一的选择。你之后可以在这系列文档中学到别的可替代的编译和部署的选择。

以下是原文,摘自:https://angular.io/docs/ts/latest/guide/setup.html。

Setup a local development environment

The  example is an Angular playground. It's not where you'd develop a real application. You 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 .

The  is QuickStart plus the app.module.ts and main.ts application files () that facilitate richer application examples.

Make sure you have . Then ...

  1. Create a project folder (you can call it quickstart and rename it later).
  2.  or  the QuickStart seed into your project folder.
  3. Install  packages.
  4. Run npm start to launch the sample application.

Clone

Perform the clone-to-launch steps with these terminal commands.

git clone https://github.com/angular/quickstart.git quickstartcd quickstartnpm installnpm start

Download

 and unzip it into your project folder. Then perform the remaining steps with these terminal commands.

cd quickstartnpm installnpm start

What's in the QuickStart seed?

The QuickStart seed contains the same application as the QuickStart playground. But it's true purpose is to provide a solid foundation for local development. Consequently, there are many more files in the project folder on your machine, most of which you can .

Focus on the following three TypeScript (.ts) files in the /app folder.

app
app.component.ts
app.module.ts
main.ts
import {
Component } from '@angular/core';@Component({
selector: 'my-app', template: `

Hello {
{name}}

`})export class AppComponent {
name = 'Angular'; }

All guides and cookbooks have at least these core files. Each file has a distinct purpose and evolves independently as the application grows.

File Purpose
app.component.ts

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.

app.module.ts

Defines AppModule, the  that tells Angular how to assemble the application. Right now it declares only the AppComponent. Soon there will be more components to declare.

main.ts

Compiles the application with the  and  the application to run in the browser. That's a reasonable choice for 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 options later in the documentation.

Next Step

If you're new to Angular, we recommend staying on the .

Appendix: node and npm

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.

 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  for managing multiple versions of node and npm. You may need  if you already have projects running on your machine that use other versions of node and npm.

Appendix: Why develop locally

 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.

The  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 ...

  • 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

Use the  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  or .

For real development, we strongly recommend .

转载地址:https://chaiyu2002.blog.csdn.net/article/details/54178576 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:AngularJS学习之三:学习Angular
下一篇:AngularJS学习之一:AngularJS的QUICKSTART

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月20日 12时02分07秒