热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Angular4.0_Auction项目依赖注入

重构Auction步骤编写ProductService.包含3个方法:getProducts(),getProducts(id)以及getCommentsForPr

重构Auction步骤


  • 编写ProductService.包含3个方法:getProducts(),getProducts(id)以及getCommentsForProduct(id)

ng g service shared/product


product.service.ts

import {Injectable} from '@angular/core';@Injectable({providedIn: 'root'
})
export class ProductService {private products: Product[] = [new Product(1, '第一个商品', 1.99, 3.5, "这是第一商品,随便是到手京东卡是你的拉克丝等你拉屎的", ["电子产品", "硬件设备", "其他"]),new Product(2, '第二个商品', 2.99, 2.5, "这是第二商品,随便是到手京东卡是你的拉克丝等你拉屎的", ["硬件设备", "其他"]),new Product(3, '第三个商品', 3.99, 1.5, "这是第三商品,随便是到手京东卡是你的拉克丝等你拉屎的", ["电子产品", "硬件设备"]),new Product(4, '第四个商品', 4.99, 2.0, "这是第四商品,随便是到手京东卡是你的拉克丝等你拉屎的", ["电子产品", "其他"]),new Product(5, '第五个商品', 5.99, 3.5, "这是第五商品,随便是到手京东卡是你的拉克丝等你拉屎的", ["电子产品", "硬件设备", "其他"]),new Product(6, '第六个商品', 6.99, 4.5, "这是第六商品,随便是到手京东卡是你的拉克丝等你拉屎的", ["电子产品", "硬件设备", "其他"])];private comments:Comment[] = [new Comment(1,1,"2017-02-02 22:22:22","张三",3,"东西不错"),new Comment(2,1,"2017-03-02 23:22:22","李四",4,"东西挺不错"),new Comment(3,1,"2017-04-02 24:22:22","王五",2,"东西不错"),new Comment(4,1,"2017-05-02 25:22:22","赵六",1,"东西还不错"),new Comment(5,1,"2017-06-02 26:22:22","哈哈",3,"东西不错"),]constructor() {}getProducts():Product[] {return this.products;}getProduct(id:number):Product{return this.products.find((product) => product.id == id);}getCommentsForProductId(id:number):Comment[]{return this.comments.filter((comment:Comment) => comment.productId == id);}
}export class Product {constructor(public id: number,public title: string,public price: number,public rating: number,public desc: string,public categories: Array<string>) {}
}export class Comment{constructor(public id:number,public productId:number,public timestamp:string,public user:string,public rating:number,public content:string){}
}

  • 修改路由配置。在从商品列表进入商品详情时&#xff0c;不再传递商品名称&#xff0c;改为传递商品ID。

修改app.module.ts

原来传的是productTitle&#xff0c;修改为productId

const routeConfig:Routes &#61; [{path:&#39;&#39;,component:HomeComponent},{path:&#39;product/:productId&#39;,component:ProductDetailComponent}
]

修改product.component.html&#xff0c;也改为id

<div *ngFor&#61;"let product of products" class&#61;"col-md-4 col-sm-4 col-lg-4"><div class&#61;"thumbnail"><img [src]&#61;"imgUrl"><div class&#61;"caption"><h4 class&#61;"pull-right">{{product.price}}h4><h4><a [routerLink]&#61;"[&#39;/product&#39;,product.id]">{{product.title}}a>h4><p>{{product.desc}}p>div><div><app-stars [rating]&#61;"product.rating">app-stars>div>div>
div>

product-detail.component.ts

import { Component, OnInit } from &#39;&#64;angular/core&#39;;
import {ActivatedRoute} from "&#64;angular/router";
import {Product} from "../shared/product.service";&#64;Component({selector: &#39;app-product-detail&#39;,templateUrl: &#39;./product-detail.component.html&#39;,styleUrls: [&#39;./product-detail.component.css&#39;]
})
export class ProductDetailComponent implements OnInit {product:Product;constructor(private routeInfo:ActivatedRoute) { }ngOnInit() {let productId:number &#61; this.routeInfo.snapshot.params["productId"];}}

  • 注入ProductService并使用其服务。

首先在app.module.ts中的providers: [ProductService],来声明这个服务。
然后在product.component.ts中注入ProductService&#xff0c;并通过ProductService来获取Products

import { Component, OnInit } from &#39;&#64;angular/core&#39;;
import {ProductService} from "../shared/product.service";&#64;Component({selector: &#39;app-product&#39;,templateUrl: &#39;./product.component.html&#39;,styleUrls: [&#39;./product.component.css&#39;]
})
export class ProductComponent implements OnInit {private products:Product[];private imgUrl &#61; &#39;http://placehold.it/320x150&#39;;constructor(private productService:ProductService) { }ngOnInit() {this.products &#61; this.productService.getProducts();}}

product-detail.component.ts

import { Component, OnInit } from &#39;&#64;angular/core&#39;;
import {ActivatedRoute} from "&#64;angular/router";
import {Product, ProductService} from "../shared/product.service";&#64;Component({selector: &#39;app-product-detail&#39;,templateUrl: &#39;./product-detail.component.html&#39;,styleUrls: [&#39;./product-detail.component.css&#39;]
})
export class ProductDetailComponent implements OnInit {product:Product;constructor(private routeInfo:ActivatedRoute,private productService:ProductService) { }ngOnInit() {let productId:number &#61; this.routeInfo.snapshot.params["productId"];this.product &#61; this.productService.getProduct(productId);}}

product-detail.component.html

<div class&#61;"thumbnail"><img src&#61;"http://placehold.it/820x230"><div><h4 class&#61;"pull-right">{{product.price}}h4><h4>{{product.title}}h4><p>{{product.desc}}p>div>div>

这里写图片描述

完成了商品详情跳转及展示&#xff0c;接下来处理商品评论

product-detail.component.ts

import {Component, OnInit} from &#39;&#64;angular/core&#39;;
import {ActivatedRoute} from "&#64;angular/router";
import {Product, ProductService, Comment} from "../shared/product.service";&#64;Component({selector: &#39;app-product-detail&#39;,templateUrl: &#39;./product-detail.component.html&#39;,styleUrls: [&#39;./product-detail.component.css&#39;]
})
export class ProductDetailComponent implements OnInit {product: Product;comments: Comment[];constructor(private routeInfo: ActivatedRoute,private productService: ProductService) {}ngOnInit() {let productId: number &#61; this.routeInfo.snapshot.params["productId"];this.product &#61; this.productService.getProduct(productId);this.comments &#61; this.productService.getCommentsForProductId(productId);}}

product-detail.component.html

<div class&#61;"thumbnail"><img src&#61;"http://placehold.it/820x230"><div><h4 class&#61;"pull-right">{{product.price}}h4><h4>{{product.title}}h4><p>{{product.desc}}p>div><div><p class&#61;"pull-right">评论&#xff1a;{{comments.length}}p><p><app-stars [rating]&#61;"product.rating">app-stars>p>div>
div><div class&#61;"well"><div class&#61;"row" *ngFor&#61;"let comment of comments"><hr><div class&#61;"col-md-12"><app-stars [rating]&#61;"comment.rating">app-stars><span>{{comment.user}}span><span class&#61;"pull-right">{{comment.timestamp}}span><p>p><p>{{comment.content}}p>div>div>
div>

这里写图片描述

gitHub参考代码


推荐阅读
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • Imtryingtofigureoutawaytogeneratetorrentfilesfromabucket,usingtheAWSSDKforGo.我正 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 本文介绍了一个题目的解法,通过二分答案来解决问题,但困难在于如何进行检查。文章提供了一种逃逸方式,通过移动最慢的宿管来锁门时跑到更居中的位置,从而使所有合格的寝室都居中。文章还提到可以分开判断两边的情况,并使用前缀和的方式来求出在任意时刻能够到达宿管即将锁门的寝室的人数。最后,文章提到可以改成O(n)的直接枚举来解决问题。 ... [详细]
  • 本文介绍了如何使用C#制作Java+Mysql+Tomcat环境安装程序,实现一键式安装。通过将JDK、Mysql、Tomcat三者制作成一个安装包,解决了客户在安装软件时的复杂配置和繁琐问题,便于管理软件版本和系统集成。具体步骤包括配置JDK环境变量和安装Mysql服务,其中使用了MySQL Server 5.5社区版和my.ini文件。安装方法为通过命令行将目录转到mysql的bin目录下,执行mysqld --install MySQL5命令。 ... [详细]
  • Spring学习(4):Spring管理对象之间的关联关系
    本文是关于Spring学习的第四篇文章,讲述了Spring框架中管理对象之间的关联关系。文章介绍了MessageService类和MessagePrinter类的实现,并解释了它们之间的关联关系。通过学习本文,读者可以了解Spring框架中对象之间的关联关系的概念和实现方式。 ... [详细]
author-avatar
开心果娟娟
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有