繁体   English   中英

在Angular 7中运行另一个Component的方法而不刷新当前页面?

[英]In Angular 7 run method of another Component without refreshing current page?

我想在另一个组件中运行一个组件的ngOnInit()而不刷新当前页面。

CompOneComponent的示例代码

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-comp-one',
  templateUrl: './comp-one.component.html',
  styleUrls: ['./comp-one.component.css']
 })

export class CompOneComponent implements OnInit {

 constructor() { }

 ngOnInit() {
     console.log("CompOneComponent ngOnInit called successfully ..");
  }

 public compOneCall() {
    console.log("CompOneComponent called successfully ..");
  }
}

parent / AppComponent的示例代码

import { Component, OnInit } from '@angular/core';
import { CompOneComponent } from './comp-one/comp-one.component';

@Component({
 selector: 'my-app',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.css'],
 providers: [CompOneComponent],
})

export class AppComponent implements OnInit {
public name = 'Angular 6';

constructor(private compOne: CompOneComponent) { }

ngOnInit() {
 this.compOne.ngOnInit();
 this.compOne.compOneCall();
 }
}

这是stackblitz的例子https://stackblitz.com/edit/hello-angular-6-tbufva

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM