Getting this error after npm start in angular project.
app/app.component.ts(12,7): error TS2304: Cannot find name 'swal'. app/app.component.ts(21,7): error TS2304: Cannot find name 'swal'.
I created an angular project. Inside app.component.ts I added sweet alert code
export class AppComponent {
deleteRow() {
swal({
title: 'Are you sure?',
text: "You won't be able to revert this!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then(function() {
swal(
'Deleted!',
'Your file has been deleted.',
'success'
);
})
}
}
I did
npm install sweetalert2 --save
and also added the path in index.html
<script src="node_modules/sweetalert2/dist/sweetalert2.min.js"></script>
<link rel="stylesheet" type="text/css" href="node_modules/sweetalert2/dist/sweetalert2.css">
SweetAlert is a method to customize alerts in your applications, websites and games. It allows the user to change it with a standard JavaScript button. You can add a new button to it, change the background color of the button, change the button's text, and add additional alerts that depend on the user's click.
Solution given by @yurzui is the only worked with angular2. I tried almost everything. Plunker may go away. i'm putting it here for others:
Plunker
1) Add these css and js on top of your index.html
<link rel="stylesheet" href="https://npmcdn.com/[email protected]/dist/sweetalert2.min.css">
<script src="https://npmcdn.com/[email protected]/dist/sweetalert2.min.js"></script>
2) Add this line to the component where you want to use swal.
declare var swal: any;
After these changes my swal namespace is available and i'm able to use it's features.
I did not import any ng2-sweelalert2
or any other module.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With