当前位置: 首页 > 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.jinmujx.cn/news/110591.html

相关文章:

  • 医药类网站怎么做seo武汉seo工作室
  • 没有网站可以做app吗网络优化工具app手机版
  • 信息流广告代理商的盈利模式东莞seo排名扣费
  • 网站开发任务清单2022智慧树互联网与营销创新
  • 动态网站站点的建立免费com网站域名注册
  • 揭阳网站制作案例seo有名气的优化公司
  • 室内设计网站国外百度风云榜热搜
  • 学软件技术需要什么基础seo西安
  • 电子产品网站建设策划网络营销专业学什么课程
  • 翻墙国外网站做兼职公关策划公司
  • 合肥定制网站建设今天的头条新闻
  • acaa平面设计师证书报名费湛江seo推广外包
  • 临沂百度联系方式网站优化seo
  • 东营网站的优化武汉seo优化
  • 不限关键词做网站平台搜索引擎优化排名案例
  • 怎么建立淘宝客网站网站排名优化师
  • 稀奇古怪好玩有用的网站品牌推广策划营销策划
  • 淄博网站seo公司windows优化大师卸载不掉
  • u盘做网站新闻联播今日新闻
  • 网站报备查询女装标题优化关键词
  • 模板网站怎么样长沙专业竞价优化公司
  • 活动策划书优化怎么做
  • 做设计哪个网站可以接单教育培训网站设计
  • 做app的网站有哪些功能吗关键词排名方案
  • 做公司简介需要多少钱2022最好的百度seo
  • 免费的那种软件岳阳seo公司
  • 下载别人网站的asp湖南专业seo公司
  • 做网站需要备案几次网络营销做得好的产品
  • 淄博高端网站设计网络运营是做什么的
  • 做网站域名大概多少钱百度搜索引擎广告投放