Angular cli 组件和服务的创建, 父传子,子传父,服务的简单使用
1:Angular cli 创建组件component
ng g component components\right ng g c wave 简写 需要定位到根路径下即可创建组件 Could not find an NgModule. Use the skip-import option to skip importing in NgModule.
PS C:\myAngulrDemos\20240428demo\mydemo01\src> cd ..
PS C:\myAngulrDemos\20240428demo\mydemo01> ng g component components\mynews
CREATE src/app/components/mynews/mynews.component.html (21 bytes)
CREATE src/app/components/mynews/mynews.component.spec.ts (626 bytes)
CREATE src/app/components/mynews/mynews.component.ts (275 bytes)
CREATE src/app/components/mynews/mynews.component.css (0 bytes)
UPDATE src/app/app.module.ts (486 bytes)
PS C:\myAngulrDemos\20240428demo\mydemo01>
2:Angular cli 创建服务 service
ng g service services\mydata
在Angular中,服务是一种可重用的、负责特定任务的独立功能单元,比如获取数据、分享数据或者处理业务逻辑等。下面是如何创建和使用服务的步骤,以Angular的最新实践为准: 创建服务
1:使用 @Injectable 装饰器: 所有的服务都需要使用 @Injectable() 装饰器来标记,这允许Angular的依赖注入系统识别并使用这个服务,下面为ts code:
import { Injectable } from '@angular/core'; @Injectable({
providedIn: 'root', // 这使得服务成为一个单例,并在根模块级别提供
})
export class MyService {
constructor() { } // 服务方法示例
getData(): any {
// 实现数据获取逻辑
}
} 2:提供服务: 在上面的例子中,我们使用了 providedIn: 'root',这意味着服务会在根模块级别被提供,并且在整个应用程序中作为单例存在。如果你希望更细粒度地控制服务的生命周期,可以在模块的 providers 数组中声明服务。 使用服务Service
2.1:注入服务: 要在组件、指令或其他服务中使用服务,你需要将其注入到构造函数中 ,下面为ts
import { Component } from '@angular/core';
import { MyService } from './my.service'; @Component({
selector: 'app-my-component',
template: `
<p>{{data}}</p>
`,
})
export class MyComponent {
data: any; constructor(private myService: MyService) { } ngOnInit() {
this.data = this.myService.getData();
}
}
3:==== 父传子 使用 Input 装饰器
父 app-root:html
<h1>app root组件</h1>
<hr>
<p>参数:ptocback, pfunctionback:函数带到子组件</p>
<app-myparent [ptoc]="ptocback" [pfunction]="pfunctionback"></app-myparent> //子组件
<router-outlet></router-outlet> 父:ts
import { Component } from '@angular/core'; @Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'mydemo01';
ptocback= "你好峰哥:"+Date.now().toLocaleString();
pfunctionback(){
alert("hello fengge");
}
}
子:app-myparent HTML
<p>myparent works!</p>
<h2>来自父组件的参数:{{ptoc}}</h2>
<hr> <app-mychild></app-mychild>
<button (click)="pfunctionClick()">点击获取父组件的函数</button> 子:ts
import { Component, OnInit } from '@angular/core'; import { Input } from '@angular/core'; // 使用 input 装饰器 加 字段参数来传值 ,引入 @Component({
selector: 'app-myparent',
templateUrl: './myparent.component.html',
styleUrls: ['./myparent.component.css']
}) export class MyparentComponent implements OnInit {
@Input() ptoc: any; //这里要定义为any才行,接受来自父组件的数据
@Input() pfunction() { } constructor(private data01service: Dataservice01Service) { }
ngOnInit(): void {} pfunctionClick() {
this.pfunction();
} }
4:=====子传父 使用 ViewChild装饰器
父 app-myparent:html
<p>myparent works!</p>
<hr>
<app-mychild #childData></app-mychild>
<button (click)="childDatatoParentClick()">点击获取子传父的数据</button> 父:ts
import { Component, OnInit } from '@angular/core';
import { ViewChild } from '@angular/core'; @Component({
selector: 'app-myparent',
templateUrl: './myparent.component.html',
styleUrls: ['./myparent.component.css']
}) export class MyparentComponent implements OnInit { @ViewChild("childData") childata: any;
getChildData: any=""; constructor() { } ngOnInit(): void { }
childDatatoParentClick(){
this.getChildData = this.childata.cName
alert("c->p:" + this.getChildData);
}
}
子 app-mychild:html
<p>mychild works!</p> 子 app-mychild:ts import { Component } from '@angular/core';
@Component({
selector: 'app-mychild',
templateUrl: './mychild.component.html',
styleUrls: ['./mychild.component.css']
})
export class MychildComponent implements OnInit { cName:any;
constructor() { } ngOnInit(): void {
this.cName = "我是Child组件的数据参数";
} }
5:===组件使用Service服务传值或者获取数据
1:生成组件的命令 : ng g service services\mydataService01 服务service ts code
import { Injectable } from '@angular/core'; @Injectable({
providedIn: 'root'
})
export class Dataservice01Service { constructor() { }
getData(){
return "this Data from service 01";
}
}
组件上使用 html
<button (click)="getdata01serverClick()">点击获取服务上的数据</button>\ 组件ts
import { Component, OnInit } from '@angular/core';
import { Dataservice01Service } from 'src/app/services/dataservice01.service'; //引入服务service
@Component({
selector: 'app-myparent',
templateUrl: './myparent.component.html',
styleUrls: ['./myparent.component.css']
}) export class MyparentComponent implements OnInit { constructor(private data01service: Dataservice01Service) { //注入service 服务
}
ngOnInit(): void {} getdata01serverClick() {
let ds = this.data01service.getData();
alert("来自服务的数据:" + ds);
} }
6 最后来一张测试截图

Angular cli 组件和服务的创建, 父传子,子传父,服务的简单使用的更多相关文章
- angular cli + primeNG
目录: 1.安装 angular cli 2.创建项目 3.构建路由 4.新建组件 5.组件之间的通信 6.引入primeNG 7.修改primeNG组件样式 8.问题 -------------- ...
- Angular CLI的安装及使用
安装命令行 npm install -g @angular/cli 检查命令行 ng v 使用这个命令可以检查 angular cli是否安装成功.我检查的时候发现没有安装成功,提示我使用的node. ...
- Netty 服务端创建
参考:http://blog.csdn.net/suifeng3051/article/details/28861883?utm_source=tuicool&utm_medium=refer ...
- 3D语音天气球(源码分享)——通过天气服务动态创建3D球
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 开篇废话: 这个项目准备分四部分介绍: 一:创建可旋转的"3D球":3 ...
- 3D语音天气球(源代码分享)——通过天气服务动态创建3D球
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 开篇废话: 这个项目准备分四部分介绍: 一:创建可旋转的"3D球":3 ...
- gRPC (1):入门及服务端创建和调用原理
1. RPC 入门 1.1 RPC 框架原理 RPC 框架的目标就是让远程服务调用更加简单.透明,RPC 框架负责屏蔽底层的传输方式(TCP 或者 UDP).序列化方式(XML/Json/ 二进制)和 ...
- [转]使用 Angular CLI 和 ng-packagr 构建一个标准的 Angular 组件库
使用 Angular CLI 构建 Angular 应用程序是最方便的方式之一. 项目目标 现在,我们一起创建一个简单的组件库. 首先,我们需要创建一个 header 组件.这没什么特别的,当然接下来 ...
- angular2.0入门---webStorm创建angular CLI项目
创建项目之前需要先安装angular cli,(angular是用typescript编写的,所以先安装typescript,再安装angularjs-cli).打开命令窗口输入 npm instal ...
- Angular CLI 创建你的第一个 Angular 示例程序
第一步:安装 Angular CLI 你要使用 Angular CLI 来创建项目.创建应用和库代码,并执行多种开发任务,比如测试.打包和发布. 全局安装 Angular CLI. 要想使用 npm ...
- Angular入门,开发环境搭建,使用Angular CLI创建你的第一个Angular项目
前言: 最近一直在使用阿里的NG-ZORRO(Angular组件库)开发公司后端的管理系统,写了一段时间的Angular以后发现对于我们.NET后端开发而言真是非常的友善.因此这篇文章主要是对这段时间 ...
随机推荐
- CDN 引入 axios 和 qs 及其使用方法
一些小项目,没必要搭建脚手架,直接以CDN的方式引入 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&quo ...
- #主席树,Dijkstra,哈希#CF464E The Classic Problem
题目 边权均为2的幂次的无向图,求 \(S\) 到 \(T\) 的最短路 \(n,m\leq 10^5\) 分析 最短路直接考虑用 Dijkstra,它需要维护松弛操作和堆, 那么也就是要重载加号和小 ...
- #线性dp,排列组合#洛谷 2476 [SCOI2008]着色方案
题目 分析(弱化版) 最暴力的想法就是直接维护每种颜色的个数dp, 弱化版有一个很突出的地方就是 \(c_i\leq 5\), 也就是说可以将相同个数的颜色合并按照个数dp, 设 \(dp[c1][c ...
- #排列组合#美团2018年CodeM大赛-决赛 A-Exam
题目 分析 因为第一名所在的学校一定会发喜报, 所以只有一个学校发喜报说明其它学校都没有发喜报 钦定第一名所在的学校为1,总方案要乘\(n\),那么两个1之间不可能出现两个相同的学校的学生 那么可以分 ...
- 本周二晚19:00战码先锋第5期直播丨深入理解OpenHarmony系统启动,轻松踏上设备软件开发之旅
OpenAtom OpenHarmony(以下简称"OpenHarmony")工作委员会首度发起「OpenHarmony开源贡献者计划」,旨在鼓励开发者参与OpenHarmony开 ...
- CMake 入门教程:从零开始构建 C/C++ 项目
CMake是一个跨平台的自动化构建工具,可以用于构建各种类型的项目,包括*C++.C.Python.Java*等.本文将从零开始,介绍如何使用CMake构建一个简单的C/C++项目 安装CMake 首 ...
- 高可用之战:Redis Sentinal(哨兵模式)
★ Redis24篇集合 1 背景 在我们的<Redis高可用之战:主从架构>篇章中,介绍了Redis的主从架构模式,可以有效的提升Redis服务的可用性,减少甚至避免Redis服务发生完 ...
- ASP.NET 部署常见问题及解决方案
ASP.NET 部署部署过程中常见问题及解决方案 Could not load file or assembly 'XXXXX' or one of its dependencies. Access ...
- 重学c#系列——什么是性能[外篇性能篇一]
前言 简单写一下性能的简介. 正文 什么是性能,很多时候有一个问题,那就很多人喜欢说.这个服务有很多访问,我们需要这样设计. 这是一个无法验证的指标,访问次数是多少? 响应时间是多少. 我把这归纳为自 ...
- 抓包整理————tcpdump过滤器[七]
前言 简单介绍一下tcpdump 正文 这里可以tcpdump -D 可以列出各个网卡的信息: 默认抓取eth0,也就是第一个: 还有下面的选项: -D 举例所有的网卡设备 -i 选择网卡设备 -c ...