简体   繁体   English

将jQuery插件添加到Angular 2组件

[英]Adding JQuery Plugin to Angular 2 Component

I am trying to add a snippet of jQuery code to a component that I got online. 我试图将jQuery代码片段添加到我在线的组件中。 Specifically this. 具体来说。

I already have the HTML and the CSS implemented into the code but I am having troubles getting the jQuery to load at all. 我已经在代码中实现了HTML和CSS,但是我很难让jQuery完全加载。 I used <script> tags at the bottom of the html part but it doesn't seem to work. 我在html部分的底部使用了<script>标记,但似乎不起作用。 Any ideas? 有任何想法吗?

You can use ViewChild decorator to get the reference of HTML content and then use that reference in you component inside ngAfterViewInit life cycle hook.You can use the reference any where inside your component but if want to do any kind of initialisation using jQuery then ngAfterViewInit is a suitable place to do that. 您可以使用ViewChild装饰器获取HTML内容的引用,然后在ngAfterViewInit生命周期挂钩中的组件中使用该引用。您可以在组件内部的任何位置使用引用,但是如果要使用jQuery进行任何形式的初始化,则ngAfterViewInit是一个合适的地方。

for example if the HTML template have following code. 例如,如果HTML模板具有以下代码。

<!-- my-component.html-->

<section class="cd-hero">
    <ul class="cd-hero-slider">
        <li class="selected">
            <div class="cd-full-width">
                <h2><!-- title here  --></h2>
                <p><!-- description here --></p>
                <a href="#0" class="cd-btn"><!-- btn text here --></a>
            </div> <!-- .cd-full-width -->
        </li>

        <!-- othe slides here -->

    </ul> <!-- .cd-hero-slider -->

    <div class="cd-slider-nav" #cdSliderNav >
        <nav>
            <span class="cd-marker item-1"></span>

            <ul>
                <li class="selected"><a href="#0">Intro</a></li>
                <li><a href="#0">Tech 1</a></li>
                <!-- other navigation items here -->
            </ul>
        </nav> 
    </div> <!-- .cd-slider-nav -->
</section> <!-- .cd-hero -->

then I can get the reference of cdSliderNav variable in my component as follows. 然后可以在组件中获取cdSliderNav变量的引用,如下所示。

//myComponent.ts

declare var jQuery: any

@Component({
  selector:'my-component',
  templateUrl:'my-component.html',
  styleUrls:['my-component.css']
})
export class MyComponent implements AfterViewInit{
  @ViewChild('cdSliderNav') cdSliderNav:ElementRef;

  ngAfterViewInit() {
      jQuery(this.cdSliderNav.nativeElement).on('click',
       event => {
                   //Handle click event here..
                  console.log(event);
                }
      )
  }
}

you can also declare jQuery variable inside your typings file if you don't want to declare it separately for each component file.And make sure that you have included jQuery file inside your index.html file. 如果您不想为每个组件文件分别声明它,也可以在类型文件中声明jQuery变量。并确保已将jQuery文件包含在index.html文件中。

Once we got the reference to the main div then we can use jQuery methods to select its child or do whatever we want using jQuery. 一旦获得对主div的引用,便可以使用jQuery方法选择其子级或使用jQuery做任何我们想做的事情。 for ex. 对于前。 to select li elements inside my cdSliderNav i can use the find() method. 在我的cdSliderNav选择li元素,我可以使用find()方法。

jQuery(this.cdSliderNav.nativeElement).find('li')

i hope this will help. 我希望这将有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM