mardi 16 octobre 2018

how to send message between two same-level component?

I am a newer of angular6 programmers, I meet a problem when I make a website about teaching. there are a navar bar and a create-course page.The navar bar is a component so as the create-course is another one. My problem is that when I enter the create-course pgae, the navar bar component will add a button —— "course saving button", when I click the button, it will save the course I have edited. I use the JhiEventManager's broadcast to exchange information. here is my code:

/* 1. navbar-component */

        import { JhiEventManager } from 'ng-jhipster';
        @Component({
            selector: 'jhi-course',
            templateUrl: './course.component.html',
            styleUrls: ['course.css']
        })
        export class CourseComponent implements OnInit, AfterViewInit {
           constructor( private eventManager: JhiEventManager){}
           ngOnInit() {
                this.eventManager.subscribe('COURSE_SAVE_EVENT', msg => {
                    console.log('braodcast:  ', msg);
                    if (msg.saveCourse) {
                        this.saveCourse();
                    }
                });
            saveCourse(){
               alert('save course');
            }
          }

/* 2. navbar-component */

    import { JhiLanguageHelper } from 'app/core';
        @Component({
            selector: 'jhi-navbar',
            templateUrl: './navbar.component.html',
            styleUrls: ['navbar.css']
        })
        export class NavbarComponent implements OnInit {
             constructor( private eventManager: JhiEventManager){}
             clickSaveCourseBtn(){
                    this.eventManager.broadcast({ name: 'COURSE_SAVE_EVENT', content: 'save the course', saveCourse: true, publishCourse: false })
           }
        }

But!!!!!! when I enter the course page for three times,and click the 'save course' button, it will excute saving operator three times! I just want it save one time,what should I do?




Aucun commentaire:

Enregistrer un commentaire