首页 > 其他分享 >Angular cli 组件和服务的创建, 父传子,子传父,服务的简单使用

Angular cli 组件和服务的创建, 父传子,子传父,服务的简单使用

时间:2024-04-28 17:46:08浏览次数:21  
标签:cli Component app component 父传子 组件 import Angular myparent

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 最后来一张测试截图

 

标签:cli,Component,app,component,父传子,组件,import,Angular,myparent
From: https://www.cnblogs.com/Fengge518/p/18164197

相关文章

  • .net core,.net 6使用SoapCore开发webservice接口,以及使用HttpClientFactory动态访问we
    1.使用soapCorenuget包 2.新建接口及实现2.1新建接口 2.2新建实现 2.3新建接收实体 2.4返回实体 3.接口注入使用  4.启动程序,直接访问对应的asmx地址  ......
  • 『手撕Vue-CLI』添加自定义指令
    前言经上篇『手撕Vue-CLI』添加帮助和版本号的介绍之后,已经可以在控制台中输入nue--help来查看帮助信息了,但是在帮助信息中只有--version,--help这两个指令,而vue-cli中还有很多指令,例如create,serve,build等等,所以本章将继续添加自定义指令,例如create指令。添加create......
  • eclipse 题解
    Statement给定一个圆,圆按照顺时针排布着\(2n\)个点,依次编号为\(1\simn\),其中编号为\(1\simn\)的点属于Alice,编号为\((n+1)\sim2n\)的点属于Bob。同时给出两个长度为\(n\)的序列\(A,B\)。你需要确定一个最大的正整数\(K\),使得存在\(K\)个二元组\((x_i,y_i)\)......
  • 2024新版本如何配置CLion与cubeMX开发STM32
    2024新版本如何配置CLion与cubeMX开发STM321.为什么我要在网上有很多教程的情况下再做一个新版各种大佬们给出的配置教程原本很详细,但是在时间过了这么久之后已经不完全符合现在的环境了。昨天在教群里萌新安装CLION+CUBEMX时我才发现,在配置过程中新出现的一些变化甚至没有清......
  • 通过设置nginx的client_max_body_size解决nginx+php上传大文件的问题
    通过设置nginx的client_max_body_size解决nginx+php上传大文件的问题用nginx来做webserver的时,上传大文件时需要特别注意client_max_body_size这个参数,否则会中断在nginx的请求中,在php中是无法记录到访问的. 一般上传大文件流程: 首先修改php.ini文件: 参数设置说明 fi......
  • 记录一个HttpClient超时连接配置不生效的问题排查过程
    现象首先有一个被服务由于内存有限,导致巨卡。导致调用他的服务出现线程阻塞。jstack打印线程池如下所示:开始排查解决问题第一步:检查代码看是否超时设置是否正确,因为感觉超时设置正确不可能阻塞。找到注入client的位置:发现配置没有任何问题,此时感到了一点点慌张。(内心OS:......
  • pyclipper的多边形操作(转载)
    等距离缩放多边形:参考博客:https://blog.csdn.net/jizhidexiaoming/article/details/134435885 文本检测DBnet中对标签的预处理里面需要用到这个操作:将文本标注框等距离缩放用于生成标签二值图像和阈值图像,如下所示: 备注:上图出自Dbnet论文原来python有个库pyclipper自带这......
  • 【Azure Event Hub】解决Event Hub SDK出现无法识别 com.azure.core.client.traits.To
    问题描述使用ServiceBusSDK编写消费端应用时,遇见了错误信息:SDK版本  <dependency>   <groupId>com.azure</groupId>   <artifactId>azure-messaging-eventhubs</artifactId>   <version>5.4.0</version>  </dependency>......
  • nvidia官方AI框架软件的命令行操作接口 —— NVIDIA GPU Cloud (NGC) CLI
    NVIDIAGPUCloud(NGC)CLI安装介绍地址:https://org.ngc.nvidia.com/setup/installers/cli安装好后需要输入自己的NVIDIANGC的APIKEY,该信息在下面地址中生成:https://org.ngc.nvidia.com/setup/api-key......
  • react native cli 替换安卓应用图标
    1.拿到需要替换的logo图标,最好是1024×1024尺寸。2.根据原图生成不同尺寸的logo:有很多类似功能的网站,这里我使用的是图标工厂图标工厂地址:图标生成网站首先上传图片然后可以配置一些其他选项如圆角等等:上传后会看到预览效果:3.替换项目中的logo图标:找到图标位置:项......