在 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/