I was wondering how I start the Twitter-Bootstrap from Typescript.
$('.carousel').carousel()
I had to implement jquery.d.ts to fix the $-sign call, but then I’m still getting the error that .carousel() could not be found in jquery.d.ts.
I tried to do this with bundling the javascript to a module and call it like that. But it does not seem to work.
This is my code:
carousel.d.ts
declare module 'carousel/carousel' {
var start: any;
export = start;
}
carousel.js
System.register('carousel/carousel', [], true, function () {
var carousel = function () {
function carousel() {
}
carousel.prototype.start = function () {
$('.carousel').carousel();
}
}
exports.carousel = carousel;
});
app.ts
import {Component} from "angular2/core";
import {bootstrap} from 'angular2/platform/browser';
import {Carousel} from "carousel/carousel";
@Component({
selector: "carousel",
bindings: [CarouselComponent],
templateUrl: 'carousel.html'
})
export class CarouselComponent {
start() {
carousel.start();
}
}
}
bootstrap(CarouselComponent)
Thanks for helping out.
6
Answers
In the end, I've changed my code to use the "InjectionToken". As described here: Use jQuery in Angular/Typescript without a type definition
You can simply inject the jQuery global instance and use it. For this you won't be needing any type definitions or typings.
When set correctly in your app.module.ts:
You can start using it in your components:
I would refactor your
carousel.js
file like this:Create a file “jquery-caroussel.d.ts” (and add-it to your reference.ts)
inside it:
It will say to the ts compilator than there is a methode carousel() which will be implemented later. (in the browser, by the carousel.js file.)
If you have a similar issue with another lib than carousel, there is plenty of sample of interface here:https://github.com/DefinitelyTyped/DefinitelyTyped
The problem is that you don’t have the typing definition for
carousel()
. Like you mentioned – it’s a function in Twitter-Bootstrap, but you only included the typing definitions (*.d.ts) for jQuery. You need to include them for Bootstrap the same way.You can get the full Bootstrap tying definitions from the DefinitelyTyped project, either from their GitHub or as a NuGet package. Here are the essential parts:
You can import JS files by declaring an ambient module for each library you need. You can have an
ambient.d.ts
file where you store all ambient module declarations (ie for JavaScript libraries you’d like to import, but for which you do not have the type definitions)ambient.d.ts:
any other *.ts file that needs
my-module-i-want-to-import
:After Angular’s Final Release, (for jquery.js and bootstrap.js)
1) add following npm packages
2) in tsconfig.json add following entries in types array,
And now you are good to go now.
Jquery carousel method error in Angular2/typescript