Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Global js to use in more QML files (SOLVED)



  • Hi,
    i've created a .js file that contains some functions and global variable;
    I'm using it with import...as and it works well in that file. I know that a js is stateful in a QML file in fact if i import it in the other ones, the global variable are just setted to zero; well...
    my work around is just copy the Component.qml's code into the main QML (that contains the js import) and it works very well...
    I know that i must import only one time and than refer to it in the other QML file, but how???

    Thanks to all
    Gianni



  • you can use
    @
    .pragma library
    @
    and it will make your js library stateless



  • uhm...
    i've already use it and it doesn't work for me.
    Anyway, if i use it, i must "import" in all QML file in which i need for?



  • yep, you should import it everywhere you are using it, but variables declared in this js file will be same for all places where you use this js file



  • Ok i'll try it...

    thank you!
    Gianni



  • I've added it at the top of js file...
    now i'm experiencing an error on:

    @
    if (component.status === Component.Ready)
    @

    ReferenceError: Can't find variable: Component

    Why?!



  • [quote author="AlterX" date="1311585310"]I've added it at the top of js file...
    now i'm experiencing an error on:

    @
    if (component.status === Component.Ready)
    @

    ReferenceError: Can't find variable: Component

    Why?![/quote]

    Ok i've seen that is a bug: when using ".pragma library" the object Component cannot exist because it is automatically bind with the import, but now only with stateful way!

    Anyway thanks


Log in to reply