当前位置: 首页 > news >正文

石家庄最好的网站建设公司排名成都seo技术

石家庄最好的网站建设公司排名,成都seo技术,全国安装平台有哪些,怎样做触屏版手机网站什么是 Services & Dependency Injection? chatgpt 回答: 在 Angular 中,Services 是用来提供特定功能或执行特定任务的可重用代码块。它们可以用于处理数据、执行 HTTP 请求、管理应用程序状态等。Dependency Injection(依赖注入&#…

什么是 Services & Dependency Injection?
chatgpt 回答:


在 Angular 中,Services 是用来提供特定功能或执行特定任务的可重用代码块。它们可以用于处理数据、执行 HTTP 请求、管理应用程序状态等。Dependency Injection(依赖注入)是 Angular 框架用来管理组件之间依赖关系的机制。通过依赖注入,Angular 能够将一个对象实例注入到另一个对象中,使得组件可以访问所需的服务或其他依赖项,这样可以更轻松地管理代码、提高可维护性,并促进代码重用。在 Angular 中,通过依赖注入将服务注入到组件中,使得组件能够使用这些服务的功能而无需自己创建实例或处理其生命周期。


Youtube Tutorial:

当前的 Angular app 中有 list 组件和 detail 组件,pokemons 数组放在list 组件中,这是一种非常糟糕的设计模式,即使这只是一个小小的 pokemon app。

接下来要做的是,抽象化 pokemons 数组,将其改成一个服务,之后就可以将此服务注入到 app 其他任意位置。

1 创建服务

1.1 有很多方法,例如 vscode terminal 运行命令:

ng g service <my-service-name>

1.2 或者使用 Angular Files extension:

首先在 app 文件夹下新建一个文件夹 services,右键此 services 文件夹,选择 Generate Service,服务名称设为 pokemon:
在这里插入图片描述
项目结构:

在这里插入图片描述

2 将服务加到 module 里

pokemon-base.module.ts:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { PokemonListComponent } from './pokemon-list/pokemon-list.component';
import { PokemonDetailComponent } from './pokemon-detail/pokemon-detail.component';
import { PokemonService } from '../services/pokemon.service';@NgModule({declarations: [PokemonListComponent, PokemonDetailComponent],imports: [CommonModule],exports: [PokemonListComponent, PokemonDetailComponent],providers: [PokemonService],  // providers 中增加新建 service
})
export class PokemonBaseModule {}

3 service 里创建整个 app 都可使用的函数

pokeman.service.ts

import { Injectable } from '@angular/core';
import { Pokemon } from '../models/pokemon';@Injectable({providedIn: 'root',
})
export class PokemonService {constructor() {}// 此函数整个 app 都可以使用getPokemons(): Pokemon[] {return [// Pokemon: 精灵宝可梦{id: 1,name: 'pikachu', // 皮卡丘type: 'electric',isCool: false,isStylish: true,},{id: 2,name: 'squirtle', // 杰尼龟type: 'water',isCool: true,isStylish: true,},{id: 3,name: 'charmander', // 小火龙type: 'fire',isCool: true,isStylish: false,},];}
}

同时,删除 pokeman-list.component.ts 中对应的 pokemons 数组。

4 依赖注入

需要用到依赖注入时,就将依赖注入放进构造函数中。

pokeman-list.component.ts 完整代码如下:

import { Component, OnInit } from '@angular/core';
import { Pokemon } from 'src/app/models/pokemon';
import { PokemonService } from 'src/app/services/pokemon.service';@Component({selector: 'app-pokemon-list',templateUrl: './pokemon-list.component.html',styleUrls: ['./pokemon-list.component.css'],
})
export class PokemonListComponent implements OnInit {pokemons!: Pokemon[];// 修改 constructorconstructor(private pokemonService: PokemonService) {}handleRemove(event: Pokemon) {this.pokemons = this.pokemons.filter((pokemon: Pokemon) => {return pokemon.id !== event.id;});}ngOnInit(): void {// 填充 pokemons 属性this.pokemons = this.pokemonService.getPokemons();}
}

运行 ng serve, 可以删除 pokemon, console 也没有错误, 这说明服务确实在运行。

在这里插入图片描述

http://www.hkea.cn/news/939882/

相关文章:

  • 慈溪外贸公司网站网络营销就业前景和薪水
  • 电商网站建设实训报告长沙网站seo推广公司
  • 阿里云ecs怎么建网站吉林网站seo
  • 企业营销型网站建设的可行性西安竞价托管
  • 做网站如何适应分辨率网站分析培训班
  • 现在币圈有那些私募网站做的好百度推广账号登陆入口
  • 旅游网站图片营销公司排名
  • 做服务器的网站都有哪些搜狗关键词排名此会zjkwlgs
  • php动态网站开发 唐四薪 答案b站引流推广网站
  • 长沙3天2晚自由行攻略论述搜索引擎优化的具体措施
  • 外汇局网站做结汇申报被逆冬seo课程欺骗了
  • 网站运营配置免费网站在线观看人数在哪直播
  • 什么网站做一手房好系统优化的例子
  • wordpress 插入wordseo排名点击工具
  • 网站推广易网宣seo的主要分析工具
  • 安徽网站定制最大免费广告发布平台
  • 怎么查网站有没有做404公司宣传网页怎么做
  • 靠谱营销网站开发选哪家seo的优点和缺点
  • 企业网站建设论文文献综述百度推广的广告真实可信吗
  • 企业网站优化方式广州最新消息今天
  • 旅游网站开发的意义seo优化方案案例
  • 山东做网站的公司seo数据分析哪些方面
  • 做网站用哪几个端口 比较好百度贴吧官网网页
  • 弹幕网站是怎么做的百度在线翻译
  • 独立网站域名代发关键词包收录
  • 如何建立门户网站百度可以发布广告吗
  • 网站开发毕设文献郑州网站优化顾问
  • 青海建设银行的官方网站自己制作网页的网站
  • 网站建设的图片叠加步骤过程2021年搜索引擎排名
  • 网站链接到邮箱怎么做搜索引擎推广试题