点击事件不在角度通用中触发

问题描述 投票:0回答:1

我已经将我的角度项目转换为角度通用。节点服务器正在工作并在 localhost:4000 显示项目,但是我在调用 .ts 文件中的函数的按钮上应用的单击事件以某种方式停止工作。 知道如何解决这个问题。

angular click server-side-rendering angular-universal mouseclick-event
1个回答
0
投票

点击处理程序将在服务器端和客户端执行。为了确保点击事件只会在客户端执行(例如显示菜单/div/弹出窗口等),您必须实现

isPlatformBrowser

看看这里的代码:


import { Component, Inject, OnInit, PLATFORM_ID } from "@angular/core";
import { isPlatformBrowser }
    from '@angular/common';

@Component({
    selector: "app-navigation",
    templateUrl: "navigation.component.html"
})
export class NavigationComponent implements OnInit {


    public isNavAsideActive: boolean;

    constructor(
        @Inject(PLATFORM_ID) private readonly _platformId: Object
    ) {
        this.isNavAsideActive = false;
    }

    public ngOnInit(): void {
        if (isPlatformBrowser(this._platformId) === true) {
            console.debug("isPlatformBrowser");
        }
    }

    public onAsideButtonClick(): void {
        if (isPlatformBrowser(this._platformId)) {
            console.debug("this.isNavAsideActive", this.isNavAsideActive);
            this.isNavAsideActive = !this.isNavAsideActive;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.