【问题标题】:Alertify Error "ReferenceError: alertify is not defined " in Angular 6Angular 6 中的警报错误“ReferenceError:未定义警报”
【发布时间】:2018-09-20 08:30:43
【问题描述】:

我尝试在我的项目中使用 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;
 });
}

【问题讨论】:

  • 试试import * as alertify;
  • 脚本添加到angular.json后是否重启ng serve
  • 您能否检查给定路径上是否存在alertify.min.js
  • 我也看过那个教程,对我来说问题是 alertify 已停止,因此即使 bower 识别它,.js 文件也不会下载。您应该 google alertify.js,手动下载脚本并将它们放入您的应用程序将查找它们的文件夹中。
  • 检查路径,我认为您提供的路径不正确。你能分享你的文件夹结构吗?

标签: angular alertifyjs


【解决方案1】:
  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",

【讨论】:

    【解决方案2】:

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

    【讨论】:

    【解决方案3】:

    在我的例子中,我在 style.css 和 angular.json 上都使用了 alertify.js 而不是 alertifyjs。

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

    【讨论】:

      猜你喜欢
      • 2012-06-30
      • 2011-10-31
      • 1970-01-01
      • 2021-02-18
      • 1970-01-01
      • 2022-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多