toastr.js 如何在 Aurelia 和 Typescript 中工作?

2024-04-28

我似乎无法让这些一起工作。我正在使用 Aurelia CLI,并以类似的方式成功地对其他库(如 select2、spin、moment 和 numeric)进行了操作。但我似乎无法让 toastr 工作。这是我到目前为止所拥有的。

首先我跑了npm install toastr --save and typings install dt~toastr --global --save

In aurelia.json,在vendor-bundle.js部分,我添加了一个依赖项,如下所示:

  "jquery",
  {
    "name": "toastr",
    "path": "../node_modules/toastr/build",
    "main": "toastr.min",
    "resources": [
      "toastr.min.css"
    ],
    "deps": ["jquery"]
  }

更新:重现的完整步骤

我安装了这些工具的以下版本:node (6.3.0)、npm (3.10.3)、au (0.17.0)

打开命令提示符并键入:

au new au-toastr
3 (Custom)
2 (Typescript)
3 (Sass)
1 (configure unit testing)
1 (Visual Studio Code)
1 (create project)
1 (install project dependencies)
cd au-toastr
npm install jquery --save
npm install toastr --save
typings install dt~jquery --global --save
typings install dt~toastr --global --save

然后打开aurelia.json在编辑器中并添加

"jquery",
{
"name": "toastr",
"path": "../node_modules/toastr/build",
"main": "toastr.min",
"resources": [
"toastr.min.css"
],
"deps": ["jquery"]
}

到依赖项的底部。

注释掉第 1839 行(declare var $: cssSelectorHelper;) on typings/globals/angular-protractor/index.d.ts由于与 jquery 的 .d.ts 文件冲突。

Replace app.ts内容与

import * as toastr from 'toastr';

export class App {
  activate() {
    toastr.info('blah');
  }
}

OR

import 'toastr';

export class App {
  activate() {
    toastr.info('blah');
  }
}

Type au run在命令提示符中,然后打开浏览器并导航到命令行显示应用程序可用的 URL(通常http://localhost:9000).


尝试1

import 'toastr';

export class ViewModel {
  activate() {
    toastr.info('blah');
  }
}

Error: ReferenceError:toastr 未定义


尝试2

import {autoinject} from 'aurelia-framework';
import 'toastr';

@autoinject()
export class ViewModel {
  constructor(private toastr: Toastr) {
  }

  activate() {
    this.toastr.info('blah');
  }
}

Error: 类型错误:this.toastr.info 不是一个函数


尝试3

import * as toastr from 'toastr';

export class ViewModel {
  activate() {
    toastr.info('blah');
  }
}

Error: 类型错误:toastr.info 不是函数


尝试4

import {autoinject} from 'aurelia-framework';
import * as toastr from 'toastr';

@autoinject()
export class ViewModel {
  constructor(private toastr: Toastr) {
  }

  activate() {
    this.toastr.info('blah');
  }
}

Error: 类型错误:this.toastr.info 不是一个函数


当我运行时,以上所有内容都会正确转换au build从命令行。我没有收到任何错误。

我不知道我错过了什么或者我还可以尝试什么。任何帮助将不胜感激!


UPDATE:我的猜测是存在错误aurelia-cli或者更有可能的是,我在某种程度上错误地处理了包裹aurelia-cli加载机制。当我从他们的网站获取打字稿框架(使用 jspm 作为模块加载器)并按照上面相同的步骤操作时,toastr 工作得很好。

有什么想法可以让它与 aurelia-cli 一起使用吗?



经过大量时间和朋友的帮助,我终于能够让它发挥作用。

只需要进行一些更改 -

aurelia.json需要更新为不使用 toastr 库的缩小版本。

{
  //...
  "dependencies": [
  //...
    "jquery",
    {
      "name": "toastr",
      "path": "../node_modules/toastr",
      "main": "toastr",
      "resources": [
        "build/toastr.min.css"
      ],
      "deps": ["jquery"]
    }
  ]
}

The toastr.info('here');函数调用通常需要在附加方法中或在 DOM 中的元素可用之后进行。

要求 HTMLapp.html需要更新为

<require from="toastr/build/toastr.min.css"></require>

希望这可以帮助。


UPDATE然后,要在视图模型中使用它,您可以采用以下几种方法之一:

import * as toastr from 'toastr';

export class App {
  attached() {
    toastr.success('here');
  }
}

import { success } from 'toastr';

export class App {
  attached() {
    success('here');
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

toastr.js 如何在 Aurelia 和 Typescript 中工作? 的相关文章

随机推荐