angular - 我正在尝试在 component.ts 文件中包含 js 代码。这样做时我遇到了错误
问题描述
这是我的 component.ts 文件:
import { Component, OnInit } from '@angular/core';
declare const scrollLeft: any;
declare const reset: any;
@Component({
selector: 'app-sample',
templateUrl: './sample.component.html',
styleUrls: ['./sample.component.scss']
})
export class SampleComponent implements OnInit {
constructor() { }
ngOnInit() {
// http://www.multislider.info/
$(document).ready(function() {
$("#myCarousel").on("slide.bs.carousel", function(e) {
var $e = $(e.relatedTarget);
var idx = $e.index();
var itemsPerSlide = 4;
var totalItems = $(".carousel-item").length;
if (idx >= totalItems - (itemsPerSlide - 1)) {
var it = itemsPerSlide - (totalItems - idx);
for (var i = 0; i < it; i++) {
// append slides to end
if (e.direction == "left") {
$(".carousel-item")
.eq(i)
.appendTo(".carousel-inner");
} else {
$(".carousel-item")
.eq(0)
.appendTo($(this).find(".carousel-inner"));
}
}
}
});
});
}
// public loadScript(url: string) {
// const body = <HTMLDivElement> document.body;
// const script = document.createElement('script');
// script.innerHTML = '';
// script.src = url;
// script.async = false;
// script.defer = true;
// body.appendChild(script);
// }
}
我收到以下错误:
“TriggeredEvent”类型上不存在属性“relatedTarget”。您指的是 'delegateTarget' 吗?
“触发事件”类型上不存在属性“方向”**
请帮我修复它们。
解决方案
如果您使用的是 Angular,我建议您使用 Ngx 引导程序或 Ng 引导程序。两者都有一个角度旋转木马组件。
不过,如果您使用的是 jquery,则必须安装它。
npm i jquery
在你的 angular.json 文件中添加
...
"build": {
...
"options": {
...
"scripts": [
"node_modules/jquery/dist/jquery.min.js"
],
...
},
...
},
...
在您的组件中
import * as $ from 'jquery';
问候