Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度:刷新页面似乎会激活ngOnInit,但不会激活OnDestroy_Angular - Fatal编程技术网

Angular 角度:刷新页面似乎会激活ngOnInit,但不会激活OnDestroy

Angular 角度:刷新页面似乎会激活ngOnInit,但不会激活OnDestroy,angular,Angular,因此,我在一个论坛上工作,该论坛的一部分是我计算当前在线会员(注册会员和访客)数量的部分。当人们进入论坛时,我激活ngOninit()中的代码,将1添加到我数据库中的文档中,该文档统计当前使用论坛的人数。当我离开论坛时,它会激活Ngondestry()并减去一 将1添加到DB文档中似乎效果不错。然而,问题是当我用F5刷新页面时,它并没有减去1。我尝试添加window.onbeforeunload=()=>this.ngondstroy()在我的ngOnInit中,但问题并没有以这种方式得到解决。

因此,我在一个论坛上工作,该论坛的一部分是我计算当前在线会员(注册会员和访客)数量的部分。当人们进入论坛时,我激活ngOninit()中的代码,将1添加到我数据库中的文档中,该文档统计当前使用论坛的人数。当我离开论坛时,它会激活Ngondestry()并减去一

将1添加到DB文档中似乎效果不错。然而,问题是当我用F5刷新页面时,它并没有减去1。我尝试添加
window.onbeforeunload=()=>this.ngondstroy()在我的ngOnInit中,但问题并没有以这种方式得到解决。它不断增加成员,即使只是我刷新了几次页面。这个问题只发生在我刷新时,当我浏览我的网站时,它不是一个问题

有人能就如何解决这个问题提出建议吗?

forumCount.component.ts

export class ForumCountComponent implements OnInit, OnDestroy{

    data;
    ngUnsubscribe = new Subject();

    constructor(private authService: AuthenticationService, private forumService: ForumService){

    }

    ngOnInit(){
        let loggedIn = this.authService.isLoggedIn();

        this.forumService.forumCountPlus(loggedIn)
            .takeUntil(this.ngUnsubscribe)
            .subscribe((data) => {
                this.data = data;
            });

        window.onbeforeunload = () => this.ngOnDestroy();
    }

    ngOnDestroy(){
        console.log('activated ngOnDestroy');
        let loggedIn = this.authService.isLoggedIn();

        this.forumService.forumCountMin(loggedIn)
            .takeUntil(this.ngUnsubscribe)
            .subscribe((data) => {
                this.data = data;
                this.ngUnsubscribe.next();
                this.ngUnsubscribe.complete();
            })
    }
}
forumCount.component.html

<div class="row justify-content-center mb-5">
    <div class="col-md-8">
        <div class="card">
            <p class="card-header">Who's Online?</p>
            <div class="card-body">
                <p class="card-text">In total there are {{data?.total}} users online :: {{data?.registered}} registered and {{data?.guests}} guests
                    <br/>
                    <br/>
                    The highest amount of online users was {{data?.highestNumber.num}} on {{data?.highestNumber.date | amDateFormat:'MMMM Do YYYY, h:mm:ss a'}}</p>
            </div>
        </div>
    </div>
    <div class="col-md-2">
    </div>
</div>

谁在线

总共有{{data?.total}个在线用户:{{data?.registed}}个已注册和{data?.guests}个访客

在线用户数量最多的是{{data?.highestNumber.date | amDateFormat:'MMMM Do YYYY,h:mm:ss a'}上的{{{{data?.highestNumber.num}}

好的,伙计们

多亏了罗德里戈的建议,我才把它修好

import {Component, OnDestroy, OnInit} from "@angular/core";
import {AuthenticationService} from "../auth/auth.service";
import {ForumService} from "./forum.service";
import {Subject} from "rxjs/Subject";


@Component({
    selector: 'forum-count',
    templateUrl: 'forum-count.component.html',
    styleUrls: ['forum-count.component.css']
})

export class ForumCountComponent implements OnInit, OnDestroy{

    data;
    ngUnsubscribe = new Subject();

    constructor(private authService: AuthenticationService, private forumService: ForumService){

    }

    ngOnInit(){
        let loggedIn = this.authService.isLoggedIn();

        this.forumService.forumCountPlus(loggedIn)
            .takeUntil(this.ngUnsubscribe)
            .subscribe((data) => {
                this.data = data;
            });

        window.onbeforeunload = () => {
            this.subtractOne()
        };
    }

    subtractOne(){
        let loggedIn = this.authService.isLoggedIn();

        this.forumService.forumCountMin(loggedIn)
            .takeUntil(this.ngUnsubscribe)
            .subscribe((data) => {
                this.data = data;
                this.ngUnsubscribe.next();
                this.ngUnsubscribe.complete();
            })
    }

    ngOnDestroy(){
        this.subtractOne();
    }


}

我只是创建了这个subtractOne方法,而不是调用ngOnDestroy,现在它就像一个符咒一样工作。

也许会澄清发生了什么,应用程序将被浏览器破坏,因此在刷新页面时不会调用onDestroy。ngOnInit()将被调用。您需要离开页面以触发ngondstroy()@RodrigoFerreira,它还提到window.onbeforeunload。我认为通过这个激活服务中的某些内容比激活Ngondestory()要好?等等,我会试试。@kboul用户仍然可以刷新页面,因此我需要找到解决方案,否则计数将不正确。这是开发阶段的解决方案,我认为您不必每次都更新数据库。想象一下,有人坐在你的网站上,不停地刷新(我甚至不谈论脚本或其他花哨的东西),这就是对服务器的大量调用。尝试实现某种服务,它接受一个值并将其添加到计时器或类似的东西中,比如说5-10秒来区分值的变化。谢谢@dAxx的建议_