Incorporating external plugins with jQuery in Angular 2

My experience with Angular 2 and jQuery has been quite positive. I am eager to incorporate external libraries such as masonry-layout, but I have encountered a frustrating issue:

When trying to use jQuery(...).masonry(), I receive an error stating that it is not a function.

To make JQuery work, I utilize webpack.

new ProvidePlugin({
    jQuery: 'jquery',
    $: 'jquery',
    jquery: 'jquery',
  })

In my test.component.ts, I have the following code:

import { Component } from '@angular/core';

import 'masonry-layout';

@Component({
    selector: 'test',
    template: `
        <div class="container-fluid grid">
            <div class="grid-item">...</div>
            <div class="grid-item">...</div>
            <div class="grid-item">...</div>
            <div class="grid-item">...</div>
        </div>
    `
})
export class TestComponent {
    ngOnInit() {
        jQuery('.grid').masonry();
    }
}

If anyone has any suggestions on how to resolve this issue, I would greatly appreciate it. Thank you in advance!

Answer №1

Start by updating your import statement to the following:

import 'masonry-layout/dist/masonry.pkgd';

It appears that configuration changes may not be effective for this particular library:

new webpack.ProvidePlugin({
  jQuery: 'jquery',
  $: 'jquery',
  jquery: 'jquery',
  'window.jQuery': 'jquery' <== this line
}),

https://i.sstatic.net/r9fyu.png In order to resolve this, you can try the following approach:

window.jQuery = jQuery;
import 'masonry-layout/dist/masonry.pkgd';

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error copying cell width attribute when using border collapse

Currently, I am attempting to duplicate a table header by copying the tr HTML and then replicating the th widths. Unfortunately, this method is unsuccessful as the widths are displayed as (width minus W) pixels when border: 'Wpx' and border-colla ...

Expanding constructor in TypeScript

Can the process described in this answer be achieved using Typescript? Subclassing a Java Builder class This is the base class I have implemented so far: export class ProfileBuilder { name: string; withName(value: string): ProfileBuilder { ...

How to populate a WebGrid with a list of data using JQuery

I am working with a list of custom objects that are received through an AJAX call's success function like this: $.ajax({ type: 'POST', url: '@Url.Action("DeleteAdmin", "UserAdmin")', cache: false, d ...

What is the best way to toggle the visibility of forms based on the select element with the use of jQuery and Bootstrap

I have set up a page with 5 different forms, each hidden within a div element using the Bootstrap class .d-none. At the top of the page, there is a select input. My goal is to create a function in jQuery that will hide all forms when an option is selected ...

Navigating data attributes with jQuery

I'm encountering some difficulties with the following code, and I'd appreciate some guidance on the process, please. Below is the HTML snippet: <span data-credit-name="Name_1"><strong>1</strong> Name 1</span><br /> ...

The type 'Data' is lacking the following attributes from its definition

Being a newcomer to Angular 8, I can't figure out why this error is popping up. If you have any suggestions on how to improve the code below, please feel free to share your tips. The error message reads: Type 'Data' is missing the follo ...

Disable the form options listed on the table

I have a scenario where I have a table and a form containing a dropdown list: <table> <tr> <td class="text">Apple</td> <td class="name">John</td> </tr> <tr> <td class=" ...

What is the comparable alternative to promise<void> in observables?

I've been working with Angular using TypeScript and I'm attempting to return a promise from an observable. What is the correct way to accomplish this? So far, I have tried the following: of(EMPTY).toPromise() // error: Promise<Observable<n ...

Why doesn't Angular 6 show the results of Next right away?

Although I don't have the code on hand, this is more of a theoretical question that I believe can still be answered. In my scenario, there is a MsgService that provides messages to components. When I update or set messages in the service from one of ...

Encountering a bug in Angular 10 while attempting to assign a value from

I am having trouble updating the role of a user. In my database, I have a user table and a role table with a ManyToMany relation. I can retrieve all the values and even the correct selected value, but I am encountering issues when trying to update it. Her ...

"Building a tree structure from a one-dimensional array in Angular: A step-by-step

I would like to implement a tree structure in Angular using flat array data and I am willing to explore different packages for rendering the tree. Users should be able to click on a node to view details such as node ID and title. The tree should initially ...

Transferring input data between two HTML files in Electron with a secure login feature

Currently, I am in the process of developing an Electron app that facilitates communication between users through socket.io. The app includes a login screen where users can enter their registered email and password to access the main menu. For testing purp ...

jQuery Does Not Iterate Through Arrays

I am encountering an issue where I am trying to populate an array with images and then pass them to a variable called $image. However, when I attempt to iterate over this array, the same item is being alerted three times. Here is the problematic code snip ...

The child module invokes a function within the parent module and retrieves a result

Guardian XML <tr [onSumWeights]="sumWeights" > Algorithm sumWeights(): number { return // Calculate total value of all weights } Offspring @Input() onTotalWeights: () => number; canMakeChanges() { return this.onTota ...

Switch image upon hover within a sprite

I have a sprite that I utilize for displaying various images. Currently, my aim is to change the sprite image upon hovering using solely CSS. .sprE { background-color: transparent; background-image: url(/i/fW.png); background-repeat: no-repeat; height: 30 ...

Initializing a table with data will only function properly when a breakpoint is set

Using the bootstrap-table library, I initialize a table with data fetched via ajax request. var arr = []; var getRows = function () { $.ajax({ type: "GET", url: hostUrl, contentType: "app ...

jQuery smooth scrolling problem causing a one-second page "blinking" issue

Currently, I have implemented a smooth scrolling effect on my website using the following jQuery code: jQuery('html,body').animate({scrollTop:scrollTarget}, 1000, "swing"); Although the effect works well in general, I have noticed that when mul ...

Tips for rearranging the order of a dropdown menu (using jquery)

How can I prioritize dropdown box items based on textbox input in jQuery? I want the dropdown box to display items that start with the text entered in the textbox. For example, if the user types "1999" in the textbox, all list items starting with "1999" sh ...

modify the color of text in a row within a jquery ajax table

Is it possible to change the font color of values in a row based on a condition inside a function? Specifically, if the TotalStudent count exceeds the room capacity, can we add student information to the table with red font color? Below is my attempt using ...

Issue with Angular date field not displaying invalid input when form is submitted

I am encountering an issue with my simple form that contains only one date control. Whenever I input an invalid date like 30-Feb-2018, the control becomes invalid and my CSS style triggers a red border to indicate the error. The problem arises when the us ...