我尝试在项目中使用alertify.js。因此,我遵循了本文https://signl.uk/tutorial/index/notify-service-with-angular-5。但是当我在应用程序中单击按钮时,出现了一个错误ReferenceError: alertify is not defined
。
alertifyjs css和js文件添加到 angular.json 文件中。
alertify.service.ts
帮助我确定我的错误。
修改
我在component.ts文件中将AlertifyService导入为private alertify: AlertifyService
,在应用程序模块中将其导入import { AlertifyService } from './services/alertify.service';
,并在提供者中将AlertifyService添加为providers: [AlertifyService]
我在登录组件中使用了Alertify
login.component.ts
login(){
let email = this.loginForm.get("email").value;
let password = this.loginForm.get("password").value;
this.loading = true;
this.authService.login(email, password).subscribe(response => {
this.alertify.success('Successfully Login');
this.loading = false;
// Save data to sessionStorage
localStorage.setItem('isLoggedIn', "true");
localStorage.setItem('currentUserID', response.user.uid);
this.router.navigate(['/dashboard'])
}, error => {
this.loading = false;
});
}
答案 0 :(得分:0)
重新启动服务器。
如果出现类似
的错误 no such file or directory, open 'D:\testProject\node_modules\alertifyjs\build\alertify.min.js'
然后 更改
"../node_modules/bootstrap/dist/css/bootstrap.min.css", "../node_modules/alertifyjs/build/css/alertify.min.css",
"../node_modules/alertifyjs/build/css/themes/bootstrap.min.css",
到
"./node_modules/bootstrap/dist/css/bootstrap.min.css",
"./node_modules/alertifyjs/build/css/alertify.min.css",
"./node_modules/alertifyjs/build/css/themes/bootstrap.min.css",
答案 1 :(得分:0)
您应该在构建部分下添加到脚本部分。可能您正在添加测试部分。(在angular.json中)
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/shop",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"aot": false,
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css"
],
"scripts": [
"node_modules/alertifyjs/build/alertify.min.js"
]
},
答案 2 :(得分:0)
就我而言,我在 style.css 和 angular.json 上都使用了 alertify.js,而不是 alertifyjs。
@import "~alertify.js/build/css/themes/bootstrap.min.css";
"./node_modules/alertify.js/build/alertify.min.js"