Even thought jquery is not limited in angular2 application, and you can truly use it in some cases such as needing to including some classy FrontEnd frameworks whose functionality relies on jquery. In my problem I am including Semantic UI for my angular2 application. Im not following the Angular Integration since it is on development stage and for some reason you can run jquery in angular2 application so i guess you can go with the traditional installation and integration.
However when implementing a sticky ui, I encounter some functionality errors of semantic ui. e.g.
$('.ui.sticky').sticky();
Typescript wont allow the sticky() function yielding this error:
Property 'sticky' does not exist on type 'JQuery<HTMLElement>'.
I installed jquery by this step
$ npm install jquery --save
$ npm install @types/jquery --save-dev
I included both the semantic ui and jquery dependencies on my angular-cli styles and scripts sections like this:
"styles": [
"styles.css",
"assets/default/momentum.css",
"assets/semantic-ui/dist/semantic.min.css",
"assets/semantic-ui/dist/components/icon.css",
"assets/semantic-ui/dist/components/card.min.css",
"assets/semantic-ui/dist/components/grid.min.css",
"assets/semantic-ui/dist/components/sticky.min.css"
],
"scripts": [
"../node_modules/jquery/dist/jquery.min.js",
"assets/semantic-ui/dist/semantic.min.js",
"assets/semantic-ui/dist/components/sticky.min.js"
],
and call it in the angular application component
import * as $ from 'jquery'
It was no use at all. Here is what i did in every tries. Component in different approach and tries with error:
import { Component, AfterViewInit } from '@angular/core';
import * as $ from 'jquery'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Momentum';
constructor() { }
ngAfterViewInit(): void {
// The .sticky() has an error wrap:
// Property 'sticky' does not exist on type 'JQuery<HTMLElement>'.
$('#header-panel').sticky()
}
}
import { Component, AfterViewInit } from '@angular/core';
declare var $:any
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Momentum';
constructor() { }
ngAfterViewInit(): void {
// I got a run time error: ERROR ReferenceError: $ is not defined
$('#header-panel').sticky()
}
}
import { Component, AfterViewInit } from '@angular/core';
import * as $ from 'jquery'
interface JQuery{
sticky(): void
}
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Momentum';
constructor() { }
ngAfterViewInit(): void {
// Still the .sticky() has an error wrap:
// Property 'sticky' does not exist on type 'JQuery<HTMLElement>'.
$('#header-panel').sticky()
}
}
import { Component, AfterViewInit } from '@angular/core';
import * as $ from 'jquery'
declare global {
interface JQuery {
sticky(): void
}
}
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Momentum';
constructor() { }
ngAfterViewInit(): void {
// On run time got this error:
// ERROR TypeError: __WEBPACK_IMPORTED_MODULE_1_jquery__(...).sticky is not a function
$('#header-panel').sticky()
}
}
import { Component, AfterViewInit } from '@angular/core';
import * as $ from 'jquery'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Momentum';
constructor() { }
ngAfterViewInit(): void {
// No matter what still got an
// ERROR TypeError: __WEBPACK_IMPORTED_MODULE_1_jquery__(...).sticky is not a function
(<any> $('#header-panel')).sticky()
($('#header-panel') as any).sticky()
}
}
It doesn't really work, It drives me nut shall i give up? or there is still some solution?