Javascript 未定义角度文档

Javascript 未定义角度文档,javascript,angular,Javascript,Angular,我想使用 npm run build:ssr SSR用于服务器端渲染。 但是在构建之后,当我试图运行我的项目时,它在我的头组件中给出了一个错误 未定义文档 标题.ts mobileMenu() { const mobileMenu = document.querySelector(".mobileHeader"); mobileMenu.classList.toggle("stickymobile"); const hambar

我想使用

npm run build:ssr
SSR用于服务器端渲染。 但是在构建之后,当我试图运行我的项目时,它在我的头组件中给出了一个错误

未定义文档

标题.ts

mobileMenu() {
    const mobileMenu = document.querySelector(".mobileHeader");
    mobileMenu.classList.toggle("stickymobile");
    const hambar = document.querySelector(".icon>i");
    mobileMenu.classList.toggle("move");
    const icon = document.querySelector(".icon");
    icon.classList.toggle("open");
  }

 head() {
    const image = document.querySelector(".image>img");
    window.addEventListener("scroll", (e) => {
      const header = document.querySelector(".desktopHeader");
      if (window.pageYOffset > 25) {
        header.classList.add("sticky");
        //@ts-ignore
        image.src = "../../../assets/Logo/Dark logo.svg";
      } else {
        header.classList.remove("sticky");
        //@ts-ignore
        image.src = "../../../assets/Logo/New logo.svg";
      }
    });
  }

  ngOnInit(): void {}

  ngAfterViewInit(): void {
    this.head();
  }

如何解决此错误,请帮助

当您使用服务器端渲染时,您需要仔细使用代码进行编码。因为在服务器上运行代码时,某些事情会发生变化。其中一些是实时浏览器对象,如文档、窗口等,还有一些函数,如SetTimeout和SetInterval。服务器中不存在这些对象和函数。因此,当您在服务器中时,需要避免执行某些代码,这就是一个示例

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

  constructor(@Inject(PLATFORM_ID) platformId: Object) {
    this.isPlatFormBrowser = isPlatformBrowser(platformId);
  }



  mobileMenu() {
    if(!this.isPlatFormBrowser) return;
      // now when you are in the server this code does not be executed although in the browser it does
    const mobileMenu = document.querySelector(".mobileHeader");
   //rest of your code here...
  }


  head(){
    if(!this.isPlatFormBrowser) return;
      // now when you are in the server this code does not be executed although in the browser it does
    window.addEventListener("scroll", (e) => {
    //rest of your code here...
  }

尝试将图像名称更改为“没有空格”此选项是否可以回答您的问题?[链接]()