Import JavaScript library into "WorkerScript"



  • import "test.js" as TEST // Does not work
    .import "test.js" as TEST2 // Does also not work

    WorkerScript.onMessage = function(message) {

    [...]

    }

    How to import the library "test.js" into a JavaScript source file of a WorkerScript?


  • Moderators

    Hi @sk2212 To include Js in Js use Qt.include(). Eg: Qt.include("sample.js")



  • @p3c0

    Hmmm...working so far. JavaScript functions in library "test.js" are useable.

    But it seems that another new instance of this library is created although the first line in "test.js" is ".pragma library". Therefore I cannot use this approach for saving a global setting.

    Well, if this is not solveable this way I will use "sendMessage()" within WorkerScript to trigger onMessage function which will call the JavaScript-function in "test.js" in another QML file where I can use "import" syntax.

    Maybe this is also a "cleaner" solution?


  • Moderators

    @sk2212

    But it seems that another new instance of this library is created although the first line in "test.js" is ".pragma library". Therefore I cannot use this approach for saving a global setting.

    Yes it ignores pragmas and imports. See including-a-javascript-resource-from-another-javascript-resource for more info.

    import will only work in QML so you can go with your solution.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.