QJSEngine, how to import other scripts?
-
Did you trying importing like the follows
import "math.mjs" as AlhasniMethods AlhasniMethods.sum(5,6)
-
@dheerendra I still get the error Uncaught exception at line 1: SyntaxError: Unexpected token `import'
-
Looks like you are trying to import one javascript inside other. If this is the case true like follows.
.import "math.mjs" as AlhasniMethods
Notice the "." before import statement
-
I get a different error now: Uncaught exception at line 4: ReferenceError: AlhasniMethods is not defined
Evaluate:
.import "math.js" as AlhasniMethods AlhasniMethods.sum(5,6)
math.js:
export function sum(left, right) { return left + right }
-
Try something like this.
/* Dheeru.js */ function func1() { console.log("http://www.pthinks.com") } /* MyMain.js */ Qt.include("Dheeru.js") function func() { console.log("Method is called") func1(); } // Inside the QML import "MyMain.js" as MyFuncs
-
I am not using QML
-
Example also talks about how import one js inside another js. If you call method func() you should be able to call func1() method as well.
-
But I think those are QML specific.
For example I get this error when I evaluate with QJSEngine:
Uncaught exception at line 1: ReferenceError: Qt is not defined -
I've downloaded this lib and added this script as an "Add Existing files..." to qml.qrc. Then I tried to load the module in my own javascript file but fails, and here it is.
According to https://doc.qt.io/qt-5/qtqml-javascript-imports.html#importing-a-javascript-resource-from-another-javascript-resource I get errors while trying to import another javascript file.
I've also tried to use Qt.include() but it fails too..import "math.js" as mathematics function f() { console.log(mathematics.sqrt(-4).toString()) } ... QQmlApplicationEngine failed to load component Script qrc:/myscript.js unavailable Invalid import qualifier
Qt.include("math.js"); function f() { console.log(sqrt(-4).toString()) } ... ReferenceError: sqrt is not defined
So what should I do?