0

Here is my Angular component:

import { basketModule } from './wind'

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {



    constructor(){
    }

    ngOnInit(){
        basketModule.init()
    }

    public dataMain(){
        alert('hi')
    }
}

Here is the wind.js file which is imported above.

export var basketModule = (function () {

return {

  init: function(){
       dataMain()
    }

}
})();

When I run the above code, it returns an error that

core.es5.js:1020 ERROR ReferenceError: dataMain is not defined

How to access the Angular component method dataMain from the imported library ?

2
  • 1
    You mix up AngularJS with Angular2 better to use all files in .ts unless you want to import your old project. Commented Aug 25, 2017 at 7:06
  • 1
    do you want to use third party libs in Angular this might help Commented Aug 25, 2017 at 7:32

2 Answers 2

1

If you're using AngularCLI, you will need to add that file to the scripts section of the angular-cli.json file.

"scripts": [
     // path to script here in quotes
 ],

And regardless of whether you're using angular cli, make sure the 'allowJs' flag in your tsconfig.json file is set to true.

 {
     "compilerOptions": {
       "target": "es5",
       "sourceMap": true,
       "allowJS": true   // this one
   }
}

Then try importing the library in your component

import * as wind from './path/to/lib/wind.js'

Now you should be able to access that libraries functions using the 'wind' name

 wind.dataMain();
Sign up to request clarification or add additional context in comments.

Comments

0

If you want an A file methode in a B file so you should import A in B

But you should put your methode in a service and then import service in wind.js

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.