Tips on incorporating jstree into an Angular 2 application with TypeScript and @types/jstree

Hello, I am new to ng2 and have a question that may seem obvious to some. I recently installed the jstree library using npm in my angular-cli application by running the command npm i jstree --save. Following this, I also installed the types for jstree with npm i @types/jstree --save-dev as part of the setup process. However, when I tried to use it, I encountered difficulties. Can someone please guide me on how to properly utilize this third-party library with TypeScript?

Answer №1

To successfully make it work, I followed these steps starting with a new cli application.

npm install --save jquery jstree

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

After that, I made changes to the angular.json file or if using an older version of angular-cli, edited the angular-cli.json file. Added

"../node_modules/jstree/dist/themes/default-dark/style.min.css"
to the styles property array.

I then included the following items in the scripts property array:


"../node_modules/jquery/dist/jquery.min.js",
"../node_modules/jstree/dist/jstree.min.js"

Updated src/app/app.component.html with:

<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>

Further updates were made in src/app/app.component.ts:

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();
  }
}

These steps should guide you through! Now, you should be good to go!

Answer №2

Hey @CodingExpert, it looks like I have a solution for your issue. However, could you please provide more code examples to demonstrate how you intend to apply it?


Keep in mind that since jstree relies on jQuery, you'll need to import it as well.

You might also need to utilize the scripts configuration.

For more information, check out this reference: https://github.com/angular/angular-cli/wiki/stories-global-scripts.

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

What is the best way to integrate properties subsets into your classes?

In my code, I am working with 3 classes ... class1 { constructor(a, b, c) { this.a = a; this.b = b; this.c = c; this.toClass2 = function() { // TODO: return this as an instance of class2; // the conversion would remove the un ...

Issue encountered while utilizing JQueryUI alongside TypeScript and the definition file from DefinitelyTyped

Currently, I'm attempting to incorporate JQueryUI with TypeScript by first installing JQueryUI using npm install jquery-ui-dist, and then installing JQuery with npm install jquery. Additionally, I have included the definition files from DefinitelyType ...

The incorrect argument is being used to infer the generic type

I have implemented the NoInfer feature from the library called ts-toolbelt. However, it seems that the example provided below does not reflect its intended effect. In this scenario, the generic type is deduced from the util function as soon as it is intr ...

Modifying iframe src using click event from a separate component in Angular 10

I am looking to dynamically update the src attribute of an iframe when the menu bar is clicked. The menu bar resides in a separate component and includes a dropdown menu for changing languages. Depending on which language is selected, I want to update the ...

Ineffectiveness of ngFor in displaying content within a component in Ionic 5

Feeling a bit overwhelmed here, I've successfully created a new component and it's working perfectly. Now I want to use this component on two different pages. To make things easier, I decided to create a component module (/components/all-compone ...

Why are the class variables in my Angular service not being stored properly in the injected class?

When I console.log ("My ID is:") in the constructor, it prints out the correct ID generated by the server. However, in getServerNotificationToken() function, this.userID is returned as 'undefined' to the server and also prints as such. I am puzz ...

Tips for reverting from Angular 7 to Angular 6

I attempted to switch from angular 7 back to angular 6 by executing the following npm commands: npm uninstall -g angular-cli npm cache clean npm install -g <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="32535c55475e53401f515e5 ...

Recovering the .angular/cache folder - a step-by-step guide

Recently, I went through the process of upgrading my Angular project from version 8 to version 17. While cleaning up, I ended up deleting the '.angular' folder as I noticed that my project's compilation time had increased after the upgrade. ...

angular ngFor directive for displaying items in an array or a single element

In an angular 7 application, custom components are utilized with a common pattern where child components can expect either a single value or an array. The component's layout is determined based on a flag. Is there a way to simplify this pattern so tha ...

Error: Unable to parse string in URI within vscode API

Console LOG displays: \Users\skhan\Library\Application Support\Code\User\summary.txt The loop is used to replace the slashes. It works fine in Windows but not in Ubuntu and Mac. This is an example on OSX 10.11.6. Howev ...

determine the appropriate month for the calendar month component based on the route selected

I have developed a calendar component where I want to preselect the default month based on the route parameters received for the component. Here is the calendar: <p-calendar [maxDate]="dateTime" [(ngModel)]="selectedMonth" name=&quo ...

What is the correct way to effectively share data between components using a service?

I've been diving into learning Angular 2 and successfully implemented data sharing between sibling components using input/output functions. Check out my working example here. // Our root app component import {Component, NgModule} from '@angular ...

Guide to linking two input fields using a single datepicker

To achieve the goal of filling two input fields simultaneously, take a look at the following code snippet: <mat-form-field> <input matInput [matDatepicker]="startDate" formControlName="SaleDate" /> ...

What is the best way to set a variable as true within a pipeline?

Could someone assist me with a coding issue I'm facing? If the id is null, I need variable x to be true. I am unable to use if and else statements within the pipe. Any guidance would be greatly appreciated. private x = false; private y = false; n ...

Storing data received from httpClient.get and utilizing it in the future after reading

Searching for a solution to read and store data from a text file, I encountered the issue of variable scope. Despite my attempts to use the data across the project by creating a global variable, it still gets cleared once the variable scope ends. import ...

The Elusive Glitch: IOS Encounter with Ionic 2

VIEW PROBLEM

I am currently developing an Ionic 2 application using Angular 2. Interestingly, I have encountered a peculiar issue that only occurs on one specific page of the app, but specifically on IOS devices. Strangely enough, only the visible por ...

How can we effectively make a call to another API using the Refresh Token within an Angular Interceptor?

I needed to refresh the access token before sending an API request. The new token will be added to the previous API call within the interceptor. How can I trigger another API call in an Angular interceptor? import { Injectable } from '@angular/core&ap ...

Error message occurs during compilation of basic Vue file in Webpack

When I execute webpack watch in the VS2017 task runner, it displays the following error: ERROR in ./wwwroot/js/src/App.vue Module build failed: SyntaxError: Unexpected token { at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:373:25) ...

Oops! The system encountered a problem: the property 'modalStack' is not recognized on the type 'NgxSmartModalService'. Maybe you meant to use '_modalStack' instead?

Currently, I'm facing an issue while attempting to run ng build --prod in my Angular 6 project. I have also incorporated the NgxSmartModal package for handling modals. Unfortunately, the build process is failing and I can't seem to figure out why ...

Tips for adjusting HighCharts layout with highcharts-vue integrations

I have a fairly simple component: <template> <div> <chart v-if="!loading" ref="priceGraph" constructor-type="stockChart" :options="chartData" ...