进行一下简单的Angular入门,开发第一版股票管理系统的静态页面布局。
sudo npm install -g @angular/cli
安装Angular命令行工具,使用ng -v
命令查看ng new first-app
创建项目名为first-app的项目用来告知angular框架如何处理TypeScript类
实例代码
//组件元数据装饰器
@Component({
//元数据
selector: 'app-root',//css选择器,表示此组件可以通过app-root标签调用,组件的内容就是templateUrl定义
templateUrl: './app.component.html',//templateUrl模板,指定了一个html文件作为模板,最终在index.html中的app-root标签中显示
styleUrls: ['./app.component.css']//styleUrls指向了一组css文件,可以指定html模板中的样式
})
//通过装饰器将元数据附加到TypeScript类上
export class AppComponent {//定义了控制器,包含模板相关的所有属性和方法,与页面相关的逻辑都写在控制器中
title = 'first-app';//属性的值会通过{{title}}显示到页面中,将模板和控制器连接起来,常见方式:插值表达式{{}}
}
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
@NgModule({
declarations: [//声明组件、指令和管道
AppComponent
],
imports: [//声明此模块依赖的其他模块
BrowserModule//浏览器模块
],
providers: [],//声明模块中提供了什么服务
bootstrap: [AppComponent]//声明模块的主组件
})
export class AppModule { }
index.html是Angular应用启动时加载的文件
main.ts为Angular启动时加载的脚本
启动Angular
Edit Configuration -> + -> npm ->自定义名称并选择start脚本
启动后,Angular会自动侦测src文件夹信息的改变,任何改变都会刷新页面
将第三方类库安装到本地
npm install jquery --save npm install bootstrap --save
将两个库引入到项目中
修改angular-cli.json文件的styles和scripts
添加内容
"styles":[
"styles.css",
"../node_modules/bootstrap/dist/css/bootstrap.css"
],
"scripts":[
"../node_modules/jquery/dist/jquery.js",
"../node_modules/bootstrap/dist/js/bootstrap.js"
]
安装类型描述文件
为了让typescript认识bootstrap和jquery的方法,需要引入类型描述文件
npm install @types/jquery --save-dev npm install @types/bootstrap --save-dev
生成组件(会生成到app文件夹下)
ng g component navbar
生成navbar组件——会生成4个文件并且更新app.module.ts,将新生成的组件注册到模块中ng g component footer
生成footer组件ng g component search
生成search组件ng g component carousel
生成carouse组件ng g component product
生成product组件ng g component stars
生成stars组件执行完生成组件的命令后,app文件夹下回多出6个组件包,并且app.module.ts中的declarations中会多出组件的声明,如下
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { NavbarComponent } from './navbar/navbar.component';
import { FooterComponent } from './footer/footer.component';
import { SearchComponent } from './search/search.component';
import { ProductComponent } from './product/product.component';
import { StarsComponent } from './stars/stars.component';
import { CarouselComponent } from './carousel/carousel.component';
@NgModule({
declarations: [
AppComponent,
NavbarComponent,
FooterComponent,
SearchComponent,
ProductComponent,
StarsComponent,
CarouselComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
<app-navbar>app-navbar>
<div class="container">
<div class="row">
<div class="col-md-3">
<app-search>app-search>
div>
<div class="col-md-9">
<div class="row">
<app-carousel>app-carousel>
div>
<div class="row">
<app-product>app-product>
div>
div>
div>
div>
<app-footer>app-footer>
<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">
<div class="container">
<a class="navbar-brand" href="#">在线竞拍a>
<button type="button" class="navbar-toggler" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="navbar-toggler-icon">span>
button>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="navbar-nav ">
<li class="nav-item">
<a class="nav-link" href="#">关于我们a>
li>
<li class="nav-item">
<a class="nav-link" href="#">联系我们a>
li>
<li class="nav-item">
<a class="nav-link" href="#">网站地图a>
li>
ul>
div>
div>
nav>
<div class="container">
<hr>
<footer>
<div class="row">
<div class="col-lg-12">
<p>Made By Jack 2018p>
div>
div>
footer>
div>
<form name="searchForm" role="form">
<div class="form-group">
<label for="productTitle">商品名称:label>
<input type="text" id="productTitle" placeholder="商品名称" class="form-control">
div>
<div class="form-group">
<label for="productPrice">商品价格:label>
<input type="text" id="productPrice" placeholder="商品价格" class="form-control">
div>
<div class="form-group">
<label for="productCatagory">商品类别:label>
<select id="productCatagory" class="form-control">select>
div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block">搜索button>
div>
form>
<div id="demo" class="carousel slide" data-ride="carousel">
<ul class="carousel-indicators">
<li data-target="#demo" data-slide-to="0" class="active">li>
<li data-target="#demo" data-slide-to="1">li>
<li data-target="#demo" data-slide-to="2">li>
ul>
<div class="carousel-inner">
<div class="carousel-item active">
<img src="http://static.runoob.com/images/mix/img_fjords_wide.jpg" class="slide-image">
div>
<div class="carousel-item">
<img src="http://static.runoob.com/images/mix/img_nature_wide.jpg" class="slide-image">
div>
<div class="carousel-item">
<img src="http://static.runoob.com/images/mix/img_mountains_wide.jpg" class="slide-image">
div>
div>
<a class="carousel-control-prev" href="#demo" data-slide="prev">
<span class="carousel-control-prev-icon">span>
a>
<a class="carousel-control-next" href="#demo" data-slide="next">
<span class="carousel-control-next-icon">span>
a>
div>
//模板代码
<div *ngFor="let product of products" class="col-md-4 col-sm-4 col-lg-4" style="padding:20px;float: left;">
<div class="img-thumbnail">
<img [src]="imgUrl" style="width:100%">
<div class="figure-caption">
<h6 class="float-right">{{product.price}}元h6>
<h6><a href="#">{{product.title}}a>h6>
<p>{{product.desc}}p>
div>
<div>
<app-stars [rating]="product.rating">app-stars>
div>
div>
div>
//控制器代码
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-product',
templateUrl: './product.component.html',
styleUrls: ['./product.component.css']
})
export class ProductComponent implements OnInit {
public products: Array<Product>;
public imgUrl = 'https://www.baidu.com/img/bd_logo1.png?where=super';
constructor() { }
// 组件被实例化的时候,此方法被调用一次,用来初始化组件中的数据
ngOnInit() {
this.products = [
new Product (1, '第一个商品', 1.99, 3.5, '这是第一个商品描述', ['电子产品', '硬件产品']),
new Product (2, '第二个商品', 2.99, 2.5, '这是第二个商品描述', ['电子产品']),
new Product (3, '第三个商品', 3.99, 0.5, '这是第三个商品描述', ['电子产品', '硬件产品']),
new Product (4, '第四个商品', 4.99, 1.5, '这是第四个商品描述', ['硬件产品']),
new Product (5, '第五个商品', 5.99, 2.5, '这是第五个商品描述', ['电子产品', '硬件产品']),
new Product (6, '第六个商品', 6.99, 4.5, '这是第六个商品描述', ['电子产品']),
new Product (7, '第七个商品', 7.99, 3.5, '这是第七个商品描述', ['电子产品', '硬件产品']),
new Product (8, '第八个商品', 8.99, 1.5, '这是第八个商品描述', ['硬件产品']),
new Product (9, '第九个商品', 9.99, 2.5, '这是第九个商品描述', ['图书产品'])
];
}
}
export class Product {
constructor(
public id: number,
public title: string,
public price: number,
public rating: number,
public desc: string,
public categories: Array<string>
) {
}
}
//模板代码
<p>
<span *ngFor="let star of stars" class="glyphicon glyphicon-star"
[class.glyphicon-star-empty]="star">span>
<span>{{rating}}星span>
p>
//控制器代码
import {Component, Input, OnInit} from '@angular/core';
@Component({
selector: 'app-stars',
templateUrl: './stars.component.html',
styleUrls: ['./stars.component.css']
})
export class StarsComponent implements OnInit {
// rating用来接收产品组件传递来的评价分数
// @Input装饰器表示rating的属性值应该由它的父组件传递给它
@Input()
public rating = 0;
public stars: boolean[];
constructor() { }
ngOnInit() {
this.stars = [];
for (let i = 1; i <= 5; i++) {
this.stars.push(i > this.rating);
}
}
}