2

In my Angular-11, I have this Javascript file:

"node_modules/admin-lte/plugins/bs-stepper/js/bs-stepper.min.js",

I added it to angular.json as shown above.

import Stepper from '...';

@Component({
  selector: 'app-profile',
  templateUrl: './profile.component.html',
  styleUrls: ['./profile.component.css']
})
export class ProfileComponent implements OnInit {
  name = 'Angular';
  private stepper: Stepper;

  next() {
    this.stepper.next();
  }

  onSubmit() {
    return false;
  }

  ngOnInit() {
    this.stepper = new Stepper(document.querySelector('#stepper1'), {
      linear: false,
      animation: true
    })
  }
}

How do I import it into this component: profile.component.ts this way,

import Stepper from '...';

from the Javascript path

Thanks

user11352561
  • 2,277
  • 12
  • 51
  • 102
  • Have you tried `import Stepper from 'bs-stepper';` ? – Roman A. May 21 '21 at 09:44
  • @RomanA - When I did import Stepper from 'bs-stepper'; it didn't recognise Stepper. So, I am using AdminLTE3 which has it's own helper – user11352561 May 21 '21 at 09:47
  • import * as Stepper from 'node_modules/admin-lte/plugins/bs-stepper/js/bs-stepper.min.js;' or import Stepper from 'node_modules/admin-lte/plugins/bs-stepper/js/bs-stepper.min.js;' Have you tried this? – Roman A. May 21 '21 at 09:54
  • @RomanA. - I got this error: ot using the local TSLint version found for 'c:/xampp/htdocs/myapp/src/app/auth/profile/profile.component.ts' To enable code execution from the current workspace you must enable workspace library execution.tslint(1) – user11352561 May 21 '21 at 10:14
  • Plz check this for your latest issue https://stackoverflow.com/questions/65228384/tslint-extension-throwing-errors-in-my-angular-application-running-in-visual-stu – Roman A. May 21 '21 at 10:21

2 Answers2

2

You must first declare it in typing.d.ts and include angular.json script.

in angular.json

{
  "build" : {
      "scripts" : [
           "node_modules/admin-lte/plugins/bs-stepper/js/bs-stepper.min.js",
           ....
       ]        

in typing.d.ts

declare module 'admin-lte/plugins/bs-stepper/js/bs-stepper.min';

Note : If this is a JQuery package then you need to create an interface.

declare module 'jquery';
interface JQuery { 
  Stepper(DOM : any, options?: any): any;
}

finally you can now call it in the component.

in component

import Stepper from 'admin-lte/plugins/bs-stepper/js/bs-stepper.min';

Edit : Create a file named typing.d.ts inside the src folder. then add

/// <reference path = "typings.d.ts" />

to the top of the src/main.ts file

Onur Özkır
  • 559
  • 3
  • 12
1

As it happens there is a NPM package for bs-stepper that could be used out-of-the-box with Angular.

1. Install the package

From the project root folder, run the command

npm install bs-stepper --save

Also install bootstrap if needed

npm install bootstrap --save

2. Import the CSS

styles.css

@import '~bs-stepper/dist/css/bs-stepper.min.css';

/* Also import bootstrap if needed */
@import '~bs-stepper/dist/css/bs-stepper.min.css';

3. Use ViewChild instead of querySelector

Using document.querySelector in an Angular app would search the entire DOM whereas the element would only be present in the current component. Based on the size of the app it might incur a performance issue. Instead you could use the @ViewChild decorator with with a template reference variable

Template (*.html)

<!-- Here, `bsStepper` is the template reference variable -->

<div #bsStepper id="stepper1" class="bs-stepper">
  ...
</div>

Component (*.ts)

import { Component, AfterViewInit, ViewChild, ElementRef } from '@angular/core';
import Stepper from 'bs-stepper';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
  @ViewChild('bsStepper', { static: false }) stepperElement!: ElementRef<any>;
  public stepper!: Stepper;

  next() {
    this.stepper.next();
  }

  onSubmit() {
    return false;
  }

  ngAfterViewInit() {
    this.stepper = new Stepper(this.stepperElement.nativeElement, {
      linear: false,
      animation: true
    });
  }
}

Working example: Stackblitz

ruth
  • 29,535
  • 4
  • 30
  • 57
  • Everything was looking good until when I got this error in the component.ts: Property 'stepperElement' has no initializer and is not definitely assigned in the constructor.ts(2564) Property 'stepper' has no initializer and is not definitely assigned in the constructor.ts(2564) – user11352561 May 21 '21 at 10:32
  • @user11352561: It has been discussed [here](https://stackoverflow.com/questions/49699067/property-has-no-initializer-and-is-not-definitely-assigned-in-the-construc#comment115216132_49699097). Quick fix is to tell the TS compiler explicitly about the property: `public stepper!: Stepper;`. Note the exclamation mark in the declaration. Do it also for the `@ViewChild` property is required. I've adjusted the answer and the Stackblitz. – ruth May 21 '21 at 10:38