我正在尝试将jQuery插件集成到我的Angular CLI项目中。此插件不是节点程序包。这是日历选择器(可以在here上找到文件)
我已将这些文件添加到项目的'src'文件夹中的资产文件夹中,并将路径(样式/脚本)添加到angular.json文件中。依然没有。我已经在该项目上安装了jQuery,并使用了npm jquery插件,并且它们工作正常。
有人可以上传一个使用链接插件的Angular CLI示例吗?
下面的示例仅返回错误“ this.calendarPicker.calendarPicker不是函数”
我将非常感谢您的协助。
应用程序组件打字稿
import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import * as $ from 'jquery';
@Component({
selector: 'app-organiser-area',
templateUrl: './organiser-area.component.html',
styleUrls: ['./organiser-area.component.css']
})
export class OrganiserAreaComponent implements AfterViewInit {
@ViewChild('calendarPicker') picker: ElementRef;
calendarPicker: any;
constructor() { }
ngAfterViewInit() {
this.calendarPicker = $(this.picker.nativeElement);
const winWidth = $(window).width();
if (winWidth > 480) {
this.calendarPicker.calendarPicker({
monthNames: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
dayNames: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
useWheel: true,
callbackDelay: 100,
years: 2,
months: 3,
days: 5,
showDayArrows: true,
callback: function(cal) {
$('#selectedDate').html('Selected date: ' + cal.currentDate);
}
});
} else {
this.calendarPicker.calendarPicker({
monthNames: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
dayNames: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
useWheel: true,
callbackDelay: 100,
years: 1,
months: 3,
days: 3,
showDayArrows: true,
callback: function(cal) {
$('#selectedDate').html('Selected date: ' + cal.currentDate);
}
});
}
}
}
应用程序组件HTML
<main>
<div #calendarPicker class="calendarBox"><div id="calendar"></div></div>
</main>
答案 0 :(得分:0)
this.calendarPicker.calendarPicker不是函数
您遇到此错误,因为您的库没有角度CLI的类型定义。变量calendarPicker只是您清除的变量,因此如何使用该库。 所以首先您需要安装2个这样的软件包
npm install jquery --save
npm install @types/jquery --save
下载所需的库并将文件放入资产文件夹
然后在Architect =>的angular.json文件的脚本部分中,您需要为jquery lib添加路径。请使用您的
相应地更改路径"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
....
"styles": [
"src/assets/css/jquery.calendarPicker.css"
],
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"src/assets/js/jquery.calendarPicker.js"
]
},
然后在tsconfig.app.json中添加此
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"types": ["jquery"]
},
"exclude": ["test.ts", "**/*.spec.ts"]
}
最后将您的组件文件更改为此
import {
AfterViewInit,
Component,
ElementRef,
OnInit,
ViewChild
} from "@angular/core";
@Component({
selector: "app-organiser-area",
templateUrl: "./organiser-area.component.html",
styleUrls: ["./organiser-area.component.css"]
})
export class OrganiserAreaComponent implements AfterViewInit {
@ViewChild("calendarPicker") picker: ElementRef;
calendarPicker: any;
constructor() {}
ngAfterViewInit() {
this.calendarPicker = $(this.picker.nativeElement);
const winWidth = $(window).width();
$("#calendar").calendarPicker({
monthNames: [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
],
dayNames: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
useWheel: true,
callbackDelay: 500,
years: 1,
months: 3,
days: 4,
showDayArrows: false,
callback: function(cal) {
$("#mydate").html(cal.currentDate + "");
}
});
}
}