5

Hi I believe it's some kind of a newbee question, but I'm new to ng2 so please bear with me...

I've done:

npm i jstree --save

After I created using angular-cli application.

Then I installed:

npm i @types/jstree --save-dev

And that's on setup, then I tried to use it and no luck.

Please can someone show me how to use that 3rd party library with typescript

4
  • 1
    3rd Party Library Installation Commented Jul 8, 2017 at 14:40
  • @R.Richards doesn't work Commented Jul 8, 2017 at 15:52
  • Have you checked your package.json? .... If it contains @types/jstree in dependencies array than your 3rd party library is installed. Otherwise type manually "@types/jstree":"x.x.x"(version) and run npm install. Commented Jul 8, 2017 at 19:56
  • @ShreenilPatel Dude I've done it. It's not the first project I do. But it's doesn't load any of the jstree.min.js dependencies Commented Jul 9, 2017 at 7:56

2 Answers 2

13
+50

Here are the steps I took to get it to work. I started with a new cli application.

npm install --save jquery jstree

npm install --save-dev @types/jquery @types/jstree

I then updated the angular.json file, if you are using an older version of angular-cli, makes changes to the angular-cli.json file. I added "../node_modules/jstree/dist/themes/default-dark/style.min.css" to the styles property array.

I also added two items into the scripts property array: "../node_modules/jquery/dist/jquery.min.js", "../node_modules/jstree/dist/jstree.min.js"

I then updated src/app/app.component.html to

<div id="foo">
  <ul>
    <li>Root node 1
      <ul>
        <li>Child node 1</li>
        <li><a href="#">Child node 2</a></li>
      </ul>
    </li>
  </ul>
</div>

I also update src/app/app.component.ts to

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

declare var $: any;

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  ngOnInit(): void {
    $('#foo').jstree();
  }
}

Hope this helps!

Sign up to request clarification or add additional context in comments.

3 Comments

This is so useful for other jQuery components too!
Its not working properly : stackblitz.com/edit/jstree-demo Can you help , where i am wrong ?
@N.K Honestly, I'm not sure...sorry. I do have a full repo on GitHub that might help you though. It has two ways of accomplishing this. One is similar to my answer above(all of the relevant lines are commented out). The uncommented out version though is probably the more correct approach.
0

@IamStalker, it seems that i know what the problem is. But can you present more code to show how you want to use it?


As jstree depends on jQuery, you have to import it too.

You may have to use scripts config.

Here is the reference: https://github.com/angular/angular-cli/wiki/stories-global-scripts.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.