javascript - 如何在 DotNet 生成的 Angular 项目中添加 JQuery?

标签 javascript angular

在 View 初始化后,我必须对我的主题进行一些初始化。 我已经实现了 AfterViewInit,我已经成功导入了 jquery:

import * as $ from 'jquery';

但现在我需要执行这个:

ngAfterViewInit() {
    $(document).trigger('nifty.ready');
}

我遇到了麻烦,因为目前看来文档尚不可知。我想我应该有另一个导入,但我无法从哪里找到? 我的整个app.component.ts:

import { Component, AfterViewInit } from '@angular/core';
import * as $ from 'jquery';

@Component({
    selector: 'app',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
    ngAfterViewInit() {
        $(document).trigger('nifty.ready');
    }
}

我得到的错误:

Exception: Call to Node module failed with error: TypeError: Cannot read property 'document' of undefined at module.exports.module.exports (E:\My\Clients\AppClientApp\dist\vendor.js:12879:12) at AppComponent.ngAfterViewInit

编辑

您可以找到here整个网络应用程序部分。

编辑2 事实上,我的印象是 jquery 根本没有正确初始化,一旦加载(没有错误),window.jquery 返回undefined...知道为什么吗?

最佳答案

您可以将文档注入(inject)组件中。

https://angular.io/docs/ts/latest/api/platform-browser/index/DOCUMENT-let.html

import { Component, AfterViewInit, Inject } from '@angular/core';
import {DOCUMENT} from "@angular/platform-browser";

@Component({
  selector: 'app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
  constructor(@Inject(DOCUMENT) private document) {
  }
}

关于javascript - 如何在 DotNet 生成的 Angular 项目中添加 JQuery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43406459/

相关文章:

javascript - Angular 错误 : [ngModel:nonassign]

javascript - 如何从jQuery中的类中选择没有标签的子元素

javascript - jquery 点击处理程序的奇怪行为

typescript - "@"符号在 "import { Component } from ' @angular/core';”声明中是什么意思?

angular - 没有为外部模块提供名称

javascript - 有没有一种方法可以让鼠标悬停在上面,这样就不会那么烦躁了?

angular - 将生成的 forkJoin 数组传递给 Angular 中的多个组件

angular - 连续休息 API http 调用 Angular 4

javascript - 错误类型错误 : Cannot read property 'city' of undefined

javascript - 强制瓷砖占用所有可用空间