
【5】 【2】Auguar改造在线竞拍应用
发布日期:2021-05-07 19:12:52
浏览次数:35
分类:原创文章
本文共 3218 字,大约阅读时间需要 10 分钟。
1 创建商品详情组件 显示商品的图片和标题
ng g component productDetail
product-detail.component.ts
import {Component, OnInit} from '@angular/core';import {ActivatedRoute} from '@angular/router';@Component({ selector: 'app-product-detail', templateUrl: './product-detail.component.html', styleUrls: ['./product-detail.component.css']})export class ProductDetailComponent implements OnInit { productTitle: string; constructor(private routeInfo: ActivatedRoute) { } ngOnInit() { this.productTitle = this.routeInfo.snapshot.params['prodTitle']; }}
product-detail.component.html
<div> <h4> <img src="http://placehold.it/820x230" alt=""> { {productTitle}} </h4></div>
2 重构代码 把轮播图组件和商品列表组件封装进新的Home组件。
ng g component home
app.component.html
<app-navbar></app-navbar><div class="container"> <div class="row"> <div class="col-md-3"> <app-search></app-search> </div> <app-home></app-home> </div></div><app-footer></app-footer>
app.component.css
清空
home.component.html
<div class="col-md-9"> <div class="row carousel-container"> <app-carousel></app-carousel> </div> <div class="row"> <app-product></app-product> </div></div>
home.component.css
.carousel-container{ margin-bottom: 40px;}
3 配置路由,在导航到商品详情时传递商品的标题参数
app.module.ts
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 {CarouselComponent} from './carousel/carousel.component';import {ProductComponent} from './product/product.component';import {StarsComponent} from './stars/stars.component';import {SearchComponent} from './search/search.component';import {ProductDetailComponent} from './product-detail/product-detail.component';import {HomeComponent} from './home/home.component';import {RouterModule, Routes} from '@angular/router';const routeConfig: Routes = [ {path: '', component: HomeComponent}, {path: 'product/:prodTitle', component: ProductDetailComponent},];@NgModule({ declarations: [ AppComponent, NavbarComponent, FooterComponent, CarouselComponent, ProductComponent, StarsComponent, SearchComponent, ProductDetailComponent, HomeComponent ], imports: [ BrowserModule, RouterModule.forRoot(routeConfig) ], providers: [], bootstrap: [AppComponent]})export class AppModule {}
4 修改App组件,根据路由显示Home组件或商品详情组件。
app.component.html
<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"> <router-outlet></router-outlet> </div> </div></div><app-footer></app-footer>
5 修改商品列表组件,给商品标题添加带routeLink指令的连接,导航到商品详情路由。
product.component.html
<div *ngFor="let product of products"class="col-md-4 col-sm-4 col-lg-4"> <div class="thumbnail"> <img src="http://placehold.it/320x150" alt=""> <div class="caption"> <h4 class="pull-right">{ {product.price}}元</h4> <h4><a [routerLink]="['product',product.title]">{ {product.title}}</a></h4> <p>{ {product.desc}}</p> </div> <div> <app-stars [rating]="product.rating"></app-stars> </div> </div></div>
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2025年04月06日 22时40分14秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
如何提升员工体验 助力企业业务增长?这个棘手的问题终于被解决了!
2019-03-04
2020 AI 产业图谱启动,勾勒中国 AI 技术与行业生态
2019-03-04
Netty4服务端入门代码示例
2019-03-04
Spring源码:prepareBeanFactory(beanFactory);方法
2019-03-04
AcWing 828. 模拟栈
2019-03-04
(20200328已解决)从docker容器内复制文件到宿主机
2019-03-04
理解Docker ulimit参数
2019-03-04
OpenAI Gym简介及初级实例
2019-03-04
int 转 CString
2019-03-04
Edit编辑框自动换行与长度
2019-03-04
Java面向对象
2019-03-04
JAVA带标签的break和continue
2019-03-04
Java获取线程基本信息的方法
2019-03-04
vue源码分析(MVVM篇)
2019-03-04
设计模式之组合模式
2019-03-04
(Python学习笔记):字典
2019-03-04
(C++11/14/17学习笔记):线程启动、结束,创建线程多法、join,detach
2019-03-04
C++并发与多线程(一)
2019-03-04
java一些基本程序
2019-03-04
vue-依赖-点击复制
2019-03-04