• angular的父子组件的通信


    父组件给子组件传值-@input

    父组件

    home.html

    <app-header [title]="title" [msg]="msg" [run]="run" [home]="this"></app-header>
    <br/>
    <hr>
    <div>我是首页组件</div>

    homt.ts

    import { Component, OnInit } from '@angular/core';
    
    @Component({
      selector: 'app-home',
      templateUrl: './home.component.html',
      styleUrls: ['./home.component.scss']
    })
    export class HomeComponent implements OnInit {
      public title:string="首页组件的标题";
      public msg:string="我是父组件的msg";
      constructor() { }
    
      ngOnInit() {
      }
      run(){
        alert("我是父组件的run方法");
      }
    }

    子组件header:

    header.html

    <p>头部组件!---{{msg}}</p>
    
    <button (click)="getParentMsg()">子组件里面获取父组件传入的msg</button>
    <br/>
    <button (click)="getParentRun()">子组件里面执行父组件的方法</button>

    header.ts

    import { Component, OnInit,Input } from '@angular/core';
    
    @Component({
      selector: 'app-header',
      templateUrl: './header.component.html',
      styleUrls: ['./header.component.scss']
    })
    export class HeaderComponent implements OnInit {
      @Input() title:any;
      @Input() msg:any;
      @Input() run:any;
      @Input() home:any;
      constructor() { }
    
      ngOnInit() {
      }
      getParentMsg(){
        //获取父组件的数据:
        alert(this.msg);
      }
      getParentRun(){
        // this.run();
        alert(this.home.msg);
        this.home.run();
      }
    }

     父组件获取子组件的属性和方法:

    父组件new.html

    <app-footer #footer></app-footer>
    <br/>
    <hr>
    <div>我是一个新闻组件</div>
    
    <button (click)="getChildMsg()">获取子组件的msg</button>
    
    <br/>
    <button (click)="getChildRun()">执行子组件的run方法</button>

    new.ts

    import { Component, OnInit,ViewChild} from '@angular/core';
    
    @Component({
      selector: 'app-news',
      templateUrl: './news.component.html',
      styleUrls: ['./news.component.scss']
    })
    export class NewsComponent implements OnInit {
      @ViewChild('footer') footer:any;
      constructor() { }
    
      ngOnInit() {
      }
      getChildMsg(){
        alert(this.footer.msg);
      }
      getChildRun(){
        this.footer.run();
      }
    }

    子组件:

    footer.html

    <h2>我是footer子组件</h2>

    footer.ts

    import { Component, OnInit } from '@angular/core';
    
    @Component({
      selector: 'app-footer',
      templateUrl: './footer.component.html',
      styleUrls: ['./footer.component.scss']
    })
    export class FooterComponent implements OnInit {
    
      public msg:any="子组件的msg";
      constructor() { }
    
      ngOnInit() {
      }
      run(){
        alert("我是子组件的run方法");
      }
    
    }

    二、子组件通过@Output触发父组件的方法:

    父组件:

    .html

    <app-footer (outer)="run($event)"></app-footer>

    .ts

    import { Component, OnInit} from '@angular/core';
    
    @Component({
      selector: 'app-news',
      templateUrl: './news.component.html',
      styleUrls: ['./news.component.scss']
    })
    export class NewsComponent implements OnInit {
    
      constructor() { }
    
      ngOnInit() {
      }
    
      run(e){
        console.log(e);//子组件给父组件广播的数据
        alert("我是父组件的run方法");
      }
    }

    子组件:

    .html

    <h2>我是footer子组件</h2>
    <button (click)="sendParent()">通过@Output给父组件广播数据</button>

    .ts

    import { Component, OnInit,Output,EventEmitter } from '@angular/core';
    
    @Component({
      selector: 'app-footer',
      templateUrl: './footer.component.html',
      styleUrls: ['./footer.component.scss']
    })
    export class FooterComponent implements OnInit {
      @Output() private outer=new EventEmitter();
      public msg:any="子组件的msg";
      constructor() { }
    
      ngOnInit() {
      }
      sendParent(){
        // alert("111");
        this.outer.emit("我是子组件的数据");
      }
    }

  • 相关阅读:
    上传图片并实现本地预览
    a标签传递参数
    HTTP错误 404.17–Not Found 请求的内容似乎是脚本,因而将无法有静态文件处理程序来处理
    VM虚拟机无法拖拽、粘贴、复制
    ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值
    Oracle中Clob类型处理解析:ORA-01461:仅可以插入LONG列的LONG值赋值
    Oracle获取表结构信息:表名、是否视图、字段名、类型、长度、非空、主键
    SQLServer2005,2000获取表结构:字段名、类型、长度、主键、非空、注释
    c# float和double的“坑”
    VS活动解决方案平台
  • 原文地址:https://www.cnblogs.com/yiweiyihang/p/12158895.html
Copyright © 2020-2023  润新知