Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
imports.qdoc
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3/*!
4\page qtqml-javascript-imports.html
5\meta {keywords} {qmltopic}
6\title Importing JavaScript Resources in QML
7\brief Description of how to import and use JavaScript resources in QML documents
8
9\l{qtqml-javascript-resources.html}{JavaScript resources} may be imported by
10QML documents and other JavaScript resources. JavaScript resources may be
11imported via either relative or absolute URLs. In the case of a relative URL,
12the location is resolved relative to the location of the \l {QML Documents}{QML document} or
13\l{qtqml-javascript-resources.html}{JavaScript Resource} that contains the
14import. If the script file is not accessible, an error will occur. If the
15JavaScript needs to be fetched from a network resource, the component's
16\l {QQmlComponent::status()}{status} is set to "Loading" until the script has
17been downloaded.
18
19JavaScript resources may also import QML modules and other JavaScript
20resources. The syntax of an import statement within a JavaScript resource
21differs slightly from an import statement within a QML document, which is
22documented thoroughly below.
23
24\section1 Importing a JavaScript Resource from a QML Document
25
26A QML document may import a JavaScript resource with the following syntax:
27\code
28import "ResourceURL" as Qualifier
29\endcode
30For example:
31\code
32import "jsfile.js" as Logic
33\endcode
34
35Imported JavaScript resources are always qualified using the "as" keyword. The
36qualifier for JavaScript resources must start with an uppercase letter, and must
37be unique, so there is always a one-to-one mapping between qualifiers and JavaScript
38files. (This also means qualifiers cannot be named the same as built-in JavaScript
39objects such as \c Date and \c Math).
40
41The functions defined in an imported JavaScript file are available to objects
42defined in the importing QML document, via the
43\c{"Qualifier.functionName(params)"} syntax. Functions in JavaScript resources
44may take parameters whose types can be any QML value types or
45object types, as well as normal JavaScript types. The normal
46\l{qtqml-cppintegration-data.html}{data type conversion rules} will apply to
47parameters and return values when calling such functions from QML.
48
49\section1 Imports Within JavaScript Resources
50
51In \c {QtQuick 2.0}, support has been added to allow JavaScript resources to import
52other JavaScript resources and also QML type namespaces using a variation of
53the standard QML import syntax (where all of the previously described rules and
54qualifications apply).
55
56Due to the ability of a JavaScript resource to import another script or QML
57module in this fashion in \c {QtQuick 2.0}, some extra semantics are defined:
58\list
59\li a script with import statements will not inherit the imports from the QML document that imported it (accessing Component.errorString will fail, for example)
60\li a script without import statements will inherit the imports from the QML document that imported it (accessing Component.errorString will succeed, for example)
61\li a shared script (defined as .pragma library) will not inherit the imports from QML documents even if the script does not import other scripts or modules
62\endlist
63
64The first semantic is conceptually correct, given that a particular script
65might be imported by any number of QML files. The second semantic is retained
66for the purposes of backwards-compatibility. The third semantic remains
67unchanged from the current semantics for shared scripts, but is clarified here
68in respect to the newly possible case (where the script imports other scripts
69or modules).
70
71\section2 Importing a JavaScript Resource from Another JavaScript Resource
72
73A JavaScript resource may import another in the following fashion:
74\code
75import * as MathFunctions from "factorial.mjs";
76\endcode
77Or:
78\code
79.import "filename.js" as Qualifier
80\endcode
81
82The former is standard ECMAScript syntax for importing ECMAScript modules, and
83only works from within ECMAScript modules as denoted by the \c mjs file
84extension. The latter is an extension to JavaScript provided by the \c QML
85engine and will work also with non-modules. As an extension superseded by the
86ECMAScript standard, its usage is discouraged.
87
88When a JavaScript file is imported this way, it is imported with a qualifier.
89The functions in that file are then accessible from the importing script via the
90qualifier (that is, as \tt{Qualifier.functionName(params)}).
91
92Sometimes it is desirable to have the functions made available in the importing
93context without needing to qualify them. In this case ECMAScript modules and the
94JavaScript \c import statement should be used without the \c as qualifier.
95
96For example, the QML code below left calls \c showCalculations() in \c script.mjs,
97which in turn can call \c factorial() in \c factorial.mjs, as it has included
98\c factorial.mjs using \c import.
99
100\table
101\row
102\li {1,2} \snippet qml/integrating-javascript/includejs/app.qml 0
103\li \snippet qml/integrating-javascript/includejs/script.mjs 0
104\row
105\li \snippet qml/integrating-javascript/includejs/factorial.mjs 0
106\endtable
107
108The \l{QtQml::Qt::include()} {Qt.include()} function includes one JavaScript
109file from another without using ECMAScript modules and without qualifying the
110import. It makes all functions and variables from the other file available in
111the current file's namespace, but ignores all pragmas and imports defined in
112that file. This is not a good idea as a function call should never modify the
113caller's context.
114
115\l{QtQml::Qt::include()} {Qt.include()} is deprecated and should be avoided. It
116will be removed in a future version of Qt.
117
118\section2 Importing a QML Module from a JavaScript Resource
119
120A JavaScript resource may import a QML module in the following fashion:
121\code
122.import TypeNamespace MajorVersion.MinorVersion as Qualifier
123\endcode
124
125Below you can see an example that also shows how to use the QML types from a
126module imported in javascript:
127
128\code
129.import Qt.test 1.0 as JsQtTest
130
131var importedEnumValue = JsQtTest.MyQmlObject.EnumValue3
132\endcode
133
134In particular, this may be useful in order to access functionality provided
135via a singleton type; see QML_SINGLETON for more information.
136
137Your JavaScript resource by default can access all imports of the component
138that imports the resource. It does not have access to the components's imports
139if it is declared as a stateless library (using \c{.pragma library}) or contains
140an explicit \c{.import} statement.
141
142\note The .import syntax doesn't work for scripts used in \l {WorkerScript}
143
144\sa {Defining JavaScript Resources in QML}
145
146*/