0

I'm trying to call the following function, LoadMultiSelect(), from one of my components because I am using a non-Angular library:

https://ibnujakaria.github.io/multiple-select-js/

This works perfectly in the console:

new MultipleSelect('#select-multiple-language', {
  placeholder: 'Select Language'
})

And loads the JS component.

Later, I try adding it in Angular, but I cannot find how to.

I tried to export the JS function in two ways:

export default function LoadMultiSelect() {
  new MultipleSelect('#select-multiple-language', {
    placeholder: 'Select Language'
  });
}

And like this:

LoadMultiSelect() {
  new MultipleSelect('#select-multiple-language', {
    placeholder: 'Select Language'
  });
}

var multiselect = new LoadMultiSelect();

export { multiselect };

I created a file to load the exported function:

assets/js/multiselect.js

Later, I added it in my build in the scripts section from my angular.json like this:

"scripts": [
              "./node_modules/multiple-select-js/dist/js/multiple-select.js",
              "src/assets/js/multiselect.js"
            ]

And then I tried to add it in my component like this:

import LoadMultiSelect from '../../../../../assets/js/multiselect';

import LoadMultiSelect from 'src/assets/js/multiselect';

But nothing works, I get this error:

Could not find a declaration file for module '../../../../../assets/js/multiselect'. '/Users/fanmixco/Documents/GitHub/holma-bootstrap/src/assets/js/multiselect.js' implicitly has an 'any' type.

Or others, any idea what I'm doing wrong?

P.S.:

  1. Also, I tried using require, but it also failed.

  2. I already tested previous solutions with an older version of Angular:

9
  • If you plan to close it, show me how the angular 6 or earlier versions are compatible. Commented Dec 21, 2021 at 20:12
  • "I added it in my build in the scripts section from my angular.json like this: "src/assets/js/multiselect.js" " Why? Remove it. It makes no sense. Programming by guessing doesn't work. The second snippet isn't valid JavaScript/TypeScript. Commented Dec 21, 2021 at 20:28
  • This whole approach looks pretty strange. Is there a reason for this approach? What is MultipleSelect? Why do you use the extension *.js? Commented Dec 21, 2021 at 20:32
  • Hi @jabaa, I have a non-Angular JS library that I must call from my component. That's the main challenge. Commented Dec 21, 2021 at 20:33
  • Does this library already exist? Does it contain the invalid code like the second snippet? Commented Dec 21, 2021 at 20:37

1 Answer 1

1

I just tried this in my local system, with some random file like below,

export function MultipleSelect(val1, val2){
    console.log('Be awesome always', val1, ' and ', val2);
}

now I import this in my component like this,

export class AppComponent {
    title = 'stackoverflow-examples';
    declare MultipleSelect: any;

    constructor(
      private fb: FormBuilder
    ) {
    }

    ngOnInit() {
      import('./someRandomFile').then(randomFile=>{
        randomFile.MultipleSelect('one', 'two')
      });
    }
}

In order for this file to be imported in the angular ts file, I must allow it in tsconfig.json by allowing the js import as given below,

"allowJs": true

see the result in the console below,

enter image description here

Note: If unable to load the file from node_modules, please put that in regular folder like asset and do the import as suggested

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

6 Comments

I keep facing: Uncaught SyntaxError: Unexpected token 'export'
Also, it seems to be skipping this library altogether: "./node_modules/multiple-select-js/dist/js/multiple-select.js" any idea?
I had a same situation with a js library, I kept the library out from nodemodules as that was not required to be upgraded/updated in future but our functionality was dependent on it so removed it from there and kept it in assets like folder and imported it
It worked. Can you add that to your answer? Then, I'll accept it because this is a tricky topic. Thanks!
This topic is quite tricky, was tried to be closed even twice and even got -1 votes when this is a real issue in Angular 13. Thanks.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.