Angular环境搭建及简单体验
一、安装开发环境
npm install -g typescript
npm install -g @angular/cli
二、创建hello-world项目
创建项目
ng new angular2-hello-world
查看新建项目的目录结构
cd angular2-hello-world
sudo apt install tree
tree -F -L 1
.
├── angular.json
├── karma.conf.js
├── node_modules/
├── package.json
├── package-lock.json
├── README.md
├── src/
├── tsconfig.app.json
├── tsconfig.json
└── tsconfig.spec.json
2 directories, 8 files
查看src目录里的结构
cd src
tree -F
启动应用,可以在http://localhost:4200查看运行结果
ng serve
创建hello-world组件
ng-generate component hello-world
在hello-world.component.ts中定义新的组件
//导入依赖
import { Component, OnInit } from '@angular/core';
//通过注解声明控件的选择器和相关的文件url
@Component({
selector: 'app-hello-world',
templateUrl: './hello-world.component.html',
styleUrls: ['./hello-world.component.css']
})
//组件的数据模型
export class HelloWorldComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
}
在hello-world.component.html中定义模板
<p>mango, hello-world works!</p>
为了使用新增加的组件,我们把标签添加到app.component.html中。
<h1>
<app-hello-world></app-hello-world>
</h1>
执行 ng serve查看执行效果
三、创建展示用户的user-item指令
生成指令组件
mango@mango:~/angular2-hello-world$ ng generate component user-item
CREATE src/app/user-item/user-item.component.css (0 bytes)
CREATE src/app/user-item/user-item.component.html (24 bytes)
CREATE src/app/user-item/user-item.component.spec.ts (641 bytes)
CREATE src/app/user-item/user-item.component.ts (286 bytes)
UPDATE src/app/app.module.ts (585 bytes)
为组件声明并初始化一个name字段
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-user-item',
templateUrl: './user-item.component.html',
styleUrls: ['./user-item.component.css']
})
export class UserItemComponent implements OnInit {
name: string,
constructor() {
this.name = 'mango';
}
ngOnInit(): void {
}
}
在模板中显示变量name的值
<p>
{{name}} welcome into Angular world.
</p>
将app-user-item添加到app.component.html中,查看浏览器执行结果。
四、创建用户列表user-list指令
创建新组件
mango@mango:~/angular2-hello-world$ ng generate component user-list
CREATE src/app/user-list/user-list.component.css (0 bytes)
CREATE src/app/user-list/user-list.component.html (24 bytes)
CREATE src/app/user-list/user-list.component.spec.ts (641 bytes)
CREATE src/app/user-list/user-list.component.ts (286 bytes)
UPDATE src/app/app.module.ts (677 bytes)
在组件中声明并初始化names数组
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-user-list',
templateUrl: './user-list.component.html',
styleUrls: ['./user-list.component.css']
})
export class UserListComponent implements OnInit {
names: string[];
constructor() {
this.names = ['mango', 'pear', 'grap', 'apple'];
}
ngOnInit(): void {
}
}
在组件的模板中递归遍历names数组
<ul>
<li *ngFor="let name of names">Hello {{name}}</li>
</ul>
将组件添加app.component.html中,查看浏览器执行结果。
五、组合使用user-item和user-list
修改user-item的name参数使用外部输入
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-user-item',
templateUrl: './user-item.component.html',
styleUrls: ['./user-item.component.css']
})
export class UserItemComponent implements OnInit {
@Input()
name!: string;
constructor() {
}
ngOnInit(): void {
}
}
修改user-list的模板
<ul>
<app-user-item *ngFor="let name of names" [name]="name"></app-user-item>
</ul>
保存查看浏览器执行情况。
六、启动过程分析
ng会首先从angular.json中查找程序的main入口为src/main.ts
{
"outputPath": "dist/angular2-hello-world",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css"
],
"scripts": []
}
查看main.ts文件,发现启动的Module是AppModule,位于app/app.module.ts中
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.error(err));
在app.module.ts中可以看到,通过NgModule标注声明了本模块中的组件以及依赖的外部Module及作为顶层组件启动的AppComponent;
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HelloWorldComponent } from './hello-world/hello-world.component';
import { UserItemComponent } from './user-item/user-item.component';
import { UserListComponent } from './user-list/user-list.component';
@NgModule({
declarations: [
AppComponent,
HelloWorldComponent,
UserItemComponent,
UserListComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Angular环境搭建及简单体验的更多相关文章
- android Jni NDK开发环境搭建及其简单实例的编写
android Jni NDK开发环境搭建及其简单实例的编写 由于工作需要,需要采用开发想要的JNI,由于之前没有接触过安卓的开发,所以更加网上的帖子,学习了下.遇到了些问题,然后总结下学习过程中 ...
- Node.js 环境搭建及简单应用
Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境.Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型.如果你想创建自己的服务,那么Node.js是一个非 ...
- Django开发web环境搭建的简单方法(CentOS6.5环境)
这几天跟Linux下的Python + Django环境搭建卯上了.经过几天的琢磨,找到了一条自己认为给力的路径. 这里给出命令行,过程如下: 首次登陆,切换管理员: [web@bogon ~]$ s ...
- 【SSH】 之 Struts2环境搭建及简单应用开发
在上一篇文章中,我们一起了解了一下struts2的工作机制原理,接下来让我们进行一下简单应用的开发 (一)配置环境 1.建立web项目 2.导入jar包 其中struts2中有很多jar包,我们不需要 ...
- Maven仓库—Nexus环境搭建及简单介绍
1. 环境搭建 1.1 下载 http://www.sonatype.org/nexus/ NEXUS OSS [OSS = Open Source Software,开源软件--免费] NE ...
- robot framework环境搭建和简单示例
环境搭建 因为我的本机已经安装了python.selenium.pip等,所以还需安装以下程序 1.安装wxPythonhttp://downloads.sourceforge.net/wxpytho ...
- Maven仓库-Nexus环境搭建及简单介绍
1. 环境搭建 1.1 下载 http://www.sonatype.org/nexus/ NEXUS OSS [OSS = Open Source Software,开源软件——免费] NE ...
- Angular环境搭建
Angular4 随笔(一)----环境搭建 1.下载node.js 第一步:在浏览器中搜索node.js官网(https://nodejs.org/zh-cn/),根据自己系统下载相应版本,下载完成 ...
- Java Web开发SpringMVC和MyBatis框架开发环境搭建和简单有用
1.下载SpringMVC框架架包,下载地址: 点击下载 点击打开地址如图所看到的.点击下载就可以 然后把相关的jar拷贝到lib下导入 2.MyBatis(3.4.2)下载 X-Amz-Algori ...
随机推荐
- NTP时间同步服务
NTP时间服务器 作用:ntp主要是用于对计算机的时间同步管理操作. 时间是对服务器来说是很重要的,一般很多网站都需要读取服务器时间来记录相关信息,如果时间不准,则可能造成很大的影响. 部署安装NTP ...
- Selenium 3 常用 API
元素定位 获取页面元素属性 元素判断 元素操作 操作输入框/单击 双击 下拉框操作 键盘操作 鼠标操作 单选框操作 多选框操作 拖动窗口 操作 JS 框 切换 frame 使用 JS 操作页面对象 操 ...
- WSL2 Ubuntu 图形界面环境搭建(Xfce4 、XServer)
安装wsl2和Ubuntu 在安装了wsl2后有时候需要传文件到ubuntu上面,比如传一个测试项目什么的.因为wsl里面挂载了本地的磁盘,所以准备安装个图形界面,操作下也挺简单的. 关于wsl2和U ...
- 6- MySQL条件查询与通配符
-数据库表中一般包含大量的数据,很少需要查询表中的所有行,通常只会根据特定的需要提取表中的一部分数据.这个时候需要制定查询条件,及过滤条件. -在select语句中,数据库根据where字句中指定的查 ...
- IDAPython类库---idc.py的源码
#!/usr/bin/env python #--------------------------------------------------------------------- # IDAPy ...
- Linux文件共享服务之NFS
NFS(Network File System) 网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源.在NFS的应用中,本地NFS的客户端应用可 ...
- POJ3160强连通+spfa最长路(不错)
题意: 给你一个有向图,每个点上有一个权值,可正可负,然后给你一些链接关系,让你找到一个起点,从起点开始走,走过的边可以在走,但是拿过权值的点就不能再拿了,问最多能拿到多少权值? 思路: ...
- 基于MXNET框架的线性回归从零实现(房价预测为例)
1.基于MXNET框架的线性回归从零实现例子 下面博客是基于MXNET框架下的线性回归从零实现,以一个简单的房屋价格预测作为例子来解释线性回归的基本要素.这个应用的目标是预测一栋房子的售出价格(元). ...
- Python数模笔记-Sklearn(2)样本聚类分析
1.分类的分类 分类的分类?没错,分类也有不同的种类,而且在数学建模.机器学习领域常常被混淆. 首先我们谈谈有监督学习(Supervised learning)和无监督学习(Unsupervised ...
- 『政善治』Postman工具 — 9、在Postman中使用断言
目录 1.Tests的介绍 2.常用SNIPPETS(片段)说明 (1)常用变量相关 (2)状态码相关 (3)响应结果断言: (4)Header : (5)响应速度: 3.示例 (1)响应码断言 (2 ...