I work with Angular 11 Universal - server side rendering. I'm trying to implement Bootstrap 5 toasts (css works well), but it doesn't understand class new bootstrap:
angular.json - it's imported properly
"styles": [
"src/styles.scss",
"node_modules/bootstrap/dist/css/bootstrap.min.css"
],
"scripts": [
"node_modules/@popperjs/core/dist/umd/popper.min.js",
"node_modules/bootstrap/dist/js/bootstrap.min.js"
]
package.json
"@angular/platform-browser": "~11.2.7",
"@angular/platform-browser-dynamic": "~11.2.7",
"@angular/platform-server": "~11.2.7",
"@angular/router": "~11.2.7",
"@nguniversal/express-engine": "^11.2.1",
"@popperjs/core": "^2.9.2",
"bootstrap": "^5.0.0-beta3",
"express": "^4.15.2",
"popper.js": "^1.16.1",
I was trying to implement toasts with initial JS code:
import {
AfterViewInit,
Component,
ElementRef,
EventEmitter,
Inject,
Input,
OnInit,
Output,
PLATFORM_ID,
ViewChild
} from '@angular/core';
import { Toast } from '../../../../../node_modules/bootstrap/dist/js/bootstrap.min.js'
import {isPlatformBrowser} from "@angular/common";
@Component({
selector: 'app-toast',
templateUrl: './toast.component.html',
styleUrls: ['./toast.component.scss']
})
export class ToastComponent implements OnInit, AfterViewInit {
@Output() closeHit: EventEmitter<boolean> = new EventEmitter<boolean>();
// @Input() title: string = "Toast";
@Input() message: string = 'Enter message here';
@ViewChild('toast') toast: ElementRef<HTMLDivElement>
constructor(@Inject(PLATFORM_ID) private platformId: Object) {
if (isPlatformBrowser(this.platformId)) {
// var toastElList = [].slice.call(document.querySelectorAll('.toast'))
// var toastList = toastElList.map(function (toastEl) {
return new bootstrap.Toast(this.toast, {})
// })
new Toast(this.toast);
// Array.from(document.querySelectorAll('.toast'))
// .forEach(toastNode => new Toast(toastNode))
}
}
ngOnInit(): void {
}
ngAfterViewInit() {
}
}
But it don't understand class bootstrap - in new bootstrap TS2304: Cannot find name 'bootstrap'.
2 variant with importing toast directly from bootstrap.js is breaking the app new Toast(this.toast);
ReferenceError: document is not defined
A server error has occurred.
node exited with 1 code.
connect ECONNREFUSED 127.0.0.1:62043
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! client@0.0.0 dev:ssr: ng run client:serve-ssr
npm ERR! Exit status 1
Please, help! Is there any way to use Bootstrap 5 functionality for toasts, modals in Angular Universal?