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后端开发而言真是非常的友善.因此这篇文章主要是对这段时间 ...
随机推荐
- Oracle与Java JDBC数据类型对照
Oracle Database JDBC开发人员指南和参考 SQL Data Types JDBC Type Codes Standard Java Types Oracle Extension Ja ...
- KingbaseES 数据库中不同user的视图访问授权
前言 本文的目的是实现u1用户访问ud用户下的视图权限. 测试 登录system用户并创建schema,user,并授权schema的有关权限给ud用户 TEST=# select current_u ...
- Java解析json数据(fastjson2)
Json数据 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.它以易于阅读和编写的方式来表示结构化数据,常用于在不同系统之间进行数据交互和传输. JSON使 ...
- 世界疫情div界面搭建初步
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="U ...
- 5W1H聊开源之What——开源协议有哪些?
开源许可协议是指开源社区为了维护作者和贡献者的合法权利,保证软件不被一些商业机构或个人窃取,影响软件的发展而开发的协议.开源协议规定了用户在使用开源软件时的权利和责任,虽然不一定具备法律效力,但是当涉 ...
- #斜率优化,单调栈#洛谷 5504 [JSOI2011] 柠檬
题目 分析 设\(dp[i]\)表示前\(i\)个贝壳可以获得的最大收益, 则\(dp[i]=\max\{dp[j-1]+S(c[i]-c[j]+1)^2\}[s_i==s_j]\) 可以发现当且仅当 ...
- #轻重链剖分,交互#LOJ 6669 Nauuo and Binary Tree
题目 有一棵大小为\(n\)只知道根节点为1的二叉树, 可以不超过\(3*10^4\)询问两点之间距离, 最后输出除了点1以外其余点的祖先 \(n\leq 3000\) 分析 \(O(n^2)\)的时 ...
- #线段树、构造#A 或位运算
题目 一个长度为\(n\)的非负整数序列, 需要满足\(m\)个区间或值为阈值的限制条件 现在要构造一个这样的序列,不存在输出No 分析 线段树支持区间与,但查询区间或,下传标记,那就很好做了 代码 ...
- 本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现
OpenAtom OpenHarmony(以下简称"OpenHarmony")开源开发者成长计划项目自 2021 年 10 月 24 日上线以来,在开发者中引发高度关注. 成长计划 ...
- C++ 字符串完全指南:学习基础知识到掌握高级应用技巧
C++ 字符串 字符串用于存储文本. 一个字符串变量包含由双引号括起来的一组字符: 示例 创建一个 string 类型的变量并为其赋值: string greeting = "Hello&q ...