因为我是angular2的新手,我期待找到以下场景的解决方案。 获取数据后,jQuery插件无效 - http://www.owlcarousel.owlgraphic.com/
我在* var owl = jQuery(this.elementRef.nativeElement).find('#breif')上遇到了问题。 owl.owlCarousel();
我的完整代码如下所示
有角度的2分量:
/ beautify ignore:start /
import {Component, OnInit , ElementRef, Inject } from '@angular/core';
import {FORM_DIRECTIVES} from '@angular/common';
import {CAROUSEL_DIRECTIVES} from 'ng2-bootstrap/components/carousel';
/ beautify ignore:end /
import {Api} from '../../../../services/api';
declare var jQuery:any;
@Component({
selector: 'breif',
directives: [CAROUSEL_DIRECTIVES],
template: require('./template.html')
})
export class BreifComponent implements OnInit {
elementRef: ElementRef;
breifs: Object;
public myInterval:number = 5000;
public noWrapSlides:boolean = false;
public slides:Array<any> = [];
constructor(@Inject(ElementRef) elementRef: ElementRef , private api: Api) {
this.elementRef = elementRef
this.loadBreif();
}
ngOnInit() {
**var owl = jQuery(this.elementRef.nativeElement).find('#breif');
owl.owlCarousel();**
}
loadBreif(){
this.api.getBreif().subscribe(
data => {
this.breifs = data.result.articles;
},
err => console.error(err),
() => {
}
)
}
}
template.html
<div class="owl-carousel" id="breif" >
<div class="item" *ngFor="let breif of breifs"><h4>{{breif.title}}</h4></div>
答案 0 :(得分:1)
您好我发布了使用owl owl.carousel@2.1.4和angular 2.0.0 + webpack + jQuery@3.1.0的解决方法。
我遇到的一些问题是jQuery插件。
请更具体地说明异常/错误......
首先你需要通过npm或类似的方式安装上面的^包。 然后 - &gt; npm install imports-loader (因为在组件中使用owl否则你将得到fn是未定义的......因为第三方模块依赖于全局变量,如$或者这是窗口对象......)。
如果您使用的是WebPack:
imports-loader如下:
{test: /bootstrap\/dist\/js\/umd\//, loader: 'imports?jQuery=jquery'}
你也可以在webpack中使用jQuery:
var ProvidePlugin = require('webpack/lib/ProvidePlugin');
在webpack.common.js的插件部分:
plugins: [
new webpack.ProvidePlugin({
jQuery: 'jquery',
$: 'jquery',
jquery: 'jquery',
'window.jQuery': 'jquery'
})
]
对于图像加载器:
{
test: /\.(png|jpe?g|gif|ico)$/,
loader: 'file?name=public/img/[name].[hash].[ext]'
}
* public / img - images文件夹
CSS加载器:
{
test: /\.css$/,
include: helpers.root('src', 'app'),
loader: 'raw'
}
vendors.js文件应导入以下内容:
import 'jquery';
import 'owl.carousel';
import 'owl.carousel/dist/assets/owl.carousel.min.css';
请注意,owl.carousel 2仍然使用jQuery的 andSelf ()弃用函数,因此我们需要用新版本的 addBack ()替换它们。
转到owl包dist / owl.carousel.js中的node_modules文件夹: 用 - &gt;替换所有出现的和自()的回加强>()。
现在是角度2部分:
<强>猫头鹰carousel.ts:强>
import {Component} from '@angular/core';
@Component({
selector: 'carousel',
templateUrl: 'carousel.component.html',
styleUrls: ['carousel.css']
})
export class Carousel {
images: Array<string> = new Array(10);
baseUrl: string = './../../../../public/img/650x350/';
}
<强> carousel.component.ts:
强>
import { Component, Input, ElementRef, AfterViewInit, OnDestroy } from '@angular/core';
@Component({
selector: 'owl-carousel',
template: `<ng-content></ng-content>`
})
export class OwlCarousel implements OnDestroy, AfterViewInit{
@Input() options: Object;
$owlElement: any;
defaultOptions: Object = {};
constructor(private el: ElementRef) {}
ngAfterViewInit() {
for (var key in this.options) {
this.defaultOptions[key] = this.options[key];
}
var temp :any;
temp = $(this.el.nativeElement);
this.$owlElement = temp.owlCarousel(this.defaultOptions);
}
ngOnDestroy() {
this.$owlElement.data('owlCarousel').destroy();
this.$owlElement = null;
}
}
<强> carousel.component.html:强>
<owl-carousel class="owl-carousel"[options]="{navigation: true, pagination: true, rewindNav : true, items:2, autoplayHoverPause: true, URLhashListener:true}">
<div class="owl-stage" *ngFor="let img of images; let i=index">
<div class="owl-item">
<a href="#"><img src="{{baseUrl}}{{i+1}}.png"/></a>
</div>
</div>
</owl-carousel>
确保在app.module中引导所有内容:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import {OwlCarousel} from './components/carousel/carousel.component';
import {Carousel} from './components/carousel/owl-carousel';
@NgModule({
imports: [
BrowserModule,
NgbModule,
],
declarations: [
AppComponent,
OwlCarousel,
Carousel
],
providers: [appRoutingProviders],
bootstrap: [ AppComponent ]
})
export class AppModule { }