sweetalert2: Swal 8.x.x doesn't show correctly (styles are missing)
Current behavior
Hi ! We’re trying to fire a swal in an angular app.
I’ve just downloaded the latest version of sweetalert2 (8.0.1) from npm and imported it in my angular componenent (angular 6 with typescript). Then i tried to fire the swal in ngOnInit()
with a code snippet taken from sweetalert2 website, however, the swal doesnt popup, swal’s html elements are rather added to the DOM, without being styled :
We are using the standard sweetalert2 library, not ngx-sweetalert2, and it seems to be a problem with version 8 only, because rolling back to version 7.33.1 fix this issue.
Note that, I’ve not tested v8.0.1 without using angular or using ngx-sweetalert2.
Expected behavior
The sweetalert should appear normaly, as it’s demonstrated on sweetalert’s website.
Live demo
Here’s the code of my component :
import { Component, OnInit } from '@angular/core';
import Swal from 'sweetalert2';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
ngOnInit(): void {
Swal.fire(
'Good job!',
'You clicked the button!',
'success'
)
}
}
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 2
- Comments: 20 (15 by maintainers)
This would work, but here’s a slightly better solution:
Thanks for the live demo @obrassard
Yup, facing it locally too.
This helped:
Which leads me to the conclusion that angular bundling process is for some reason taking the module field from package.json instead of main field.
We made changes for v8 in package.json https://github.com/sweetalert2/sweetalert2/pull/1378 to support ES modules:
I have no idea why Angular uses
module
overmain
. Maybe @toverux have any clue about this behavior?Hello! I am using SweetAlert v11.4.4 with Vite and React on the latest version and the styles are not added to the header. I had to use import as Limote suggests and it worked
import Swal from 'sweetalert2/dist/sweetalert2.all.js';
Here’s how I solved it for
sweetalert2-webpack-demo
:webpack.config.babel.js
:I just upgraded all angular dev-deps to latest and the issue is still there.
This is the problem, please report it to the according Angular repository.
As a workaround you can use either
Or import styles separately:
I’m experiencing this issue as well with just a basic Webpack setup. Specifying the
mainFields
fixes the issue for me. Is that the desired work-around for now?How did you figure this out @limonte??? You need to tell me your secret 😃
BTW, for the record, I tried setting manually
mainFields
in./node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js
as:And the project shared by @obrassard works locally as well. I’m not sure this is the best way to configure
build-angular
(and that’s one of the main reason why I don’t like frameworks) but that may explains why the project works fine on stackblitz. Hope it helps…oops @limonte our comments crossed 😄