4

I have this code:

 <script src="node_modules/nedb/browser-version/out/nedb.min.js"> </script>

 <script>
 var server = new Nedb({filename: 'someFile2', autoload: true});
 </script>

The Nedb persistent database is created.

I just want to access the server variable in the app code, like in MyCmp.ts. How do I do this?

2 Answers 2

5

You need to declare Nedb inside Mycmp, then you should call the initialization code.

declare var Nedb: any;

@Component(...)
export class MyCmp {

    server: any;

    ngOnInit() {
       this.server = new Nedb({filename: 'someFile2', autoload: true});
    }
}

Now you can access this.server inside other functions inside Mycmp.

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

4 Comments

Thank you so much!
Where do I make this particular typings universal, so I don't need to keep declaring Nedb?
I guess my reputation is not enough.
Angular is great at dependency injection. You could initialize it in a service and then inject that into any other component that might need to use it.
0

You can easily access global variables by referencing window, like so

server = window["server"];

or

let server = window["server"];

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.