1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

Angular ReferenceError: document is not defined

Discussão em 'Angular' iniciado por user30935374, Julho 1, 2025.

  1. user30935374

    user30935374 Guest

    Note my knowledge of JS frameworks and TS is essentially non-existent.

    I'm trying to create an SPA with SSG. I have a main component I'm trying to display a webAMP in. Which needs to access document. Right now I have the code to create a webAMP object in the main-component.ts. But it's throwing this error:

    ReferenceError: document is not defined
    at eval (eval at runInlinedModule (file:.../node_modules/vite/dist/node/module-runner.js:1062:11), <anonymous>:19481:10)
    at async ESModulesEvaluator.runInlinedModule (file:.../node_modules/vite/dist/node/module-runner.js:1062:5)
    at async SSRCompatModuleRunner.directRequest (file:.../node_modules/vite/dist/node/module-runner.js:1284:61)
    at async SSRCompatModuleRunner.directRequest (file:.../node_modules/vite/dist/node/chunks/dep-DBxKXgDP.js:25274:23)
    at async SSRCompatModuleRunner.cachedRequest (file:.../node_modules/vite/dist/node/module-runner.js:1180:76)


    I think I've tried these but found no progress:


    current main.component.ts :

    import { NgOptimizedImage } from '@angular/common';
    import { Component, DOCUMENT, Inject} from '@angular/core';
    import { RouterLink, RouterOutlet } from '@angular/router';
    import Webamp from 'webamp';
    import { CommonModule } from '@angular/common';

    @Component({
    selector: 'app-main',
    imports: [RouterLink,RouterOutlet,NgOptimizedImage,CommonModule],
    templateUrl: './main.component.html',
    styleUrl: './main.component.css',

    })
    export class MainComponent{


    constructor(@Inject(DOCUMENT) private document: Document) {
    this.musicPlayer();

    }


    musicPlayer(){
    alert("the playerrrr");//for testing

    const webamp = new Webamp({
    });


    webamp.renderWhenReady(this.document.getElementById('winamp-container') as HTMLElement);

    }

    }
    }

    Continue reading...

Compartilhe esta Página