4

我尝试在我的项目中使用 alertify.js。所以我遵循了这个材料https://signl.uk/tutorial/index/notify-service-with-angular-5。但是ReferenceError: alertify is not defined在我的应用程序中单击按钮时出现错误。

我添加到angular.json文件中的alertifyjs css 和 js文件。

![在此处输入图像描述

alertify.service.ts

![在此处输入图像描述

帮助我找出我的错误。

编辑

我在 component.ts 文件中将 AlertifyService 导入为private alertify: AlertifyService和在 app 模块 import { AlertifyService } from './services/alertify.service';中,并在提供程序中添加 AlertifyService 作为 providers: [AlertifyService]

我在我的登录组件中使用 alertify

登录组件.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;
 });
}
4

3 回答 3

3
  1. 重启服务器。

  2. 如果得到像这样的错误

    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",
于 2018-09-20T10:33:21.037 回答
1

您应该添加到构建部分下的脚本部分。可能您正在添加测试部分。(在 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"

        ]
      },
于 2019-11-18T18:28:13.067 回答
0

就我而言,我在styles.css 和angular.json 上都使用了alertify.js 而不是alertifyjs。

@import "~alertify.js/build/css/themes/bootstrap.min.css"; 

"./node_modules/alertify.js/build/alertify.min.js"
于 2021-03-18T08:33:42.993 回答