Details
-
Bug
-
Resolution: Done
-
P2: Important
-
5.15.2, 6.1.2
-
-
86039f61b32095cb2e341402438c96178baa0158 (qt/qtdeclarative/dev) b93c56568613a57fb8abaacbecb6be352c2c2185 (qt/qtdeclarative/6.2)
Description
Description
JavaScript library imports are not reachable inside inline QML components i.e. those defined with component New : Existing syntax.
Here's an almost-minimal reproducible example:
main.qml
import QtQuick 2.15 import QtQuick.Controls 2.15 import QtQuick.Window 2.15 import "Lib.js" as Lib Window { width: 640 height: 480 visible: true title: qsTr("Hello World") component C : Rectangle { width: 200 height: 200 color: "#d1d1d1" // Label from Controls is reachable Label { anchors.centerIn: parent // But Lib is not text: Lib.getFruit(1) } } Column { anchors.centerIn: parent Label { text: Lib.getFruit(2) } C {} } }
Stateless JavaScript library:
Lib.js
.pragma library const FRUITS = { APPLE: 1, ORANGE: 2, }; function getFruit(n) { return Object.entries(FRUITS).find(([_, v]) => v === n)[0] }
Expected result
All imports should work, named imports are should accessible inside inline components just the same as from outside.
Actual result
qrc:/main.qml:21: TypeError: Cannot call method 'getFruit' of null
Version
Can be reproduced on both Qt 5.15.x branch and Qt 6.1.2.
Attachments
Issue Links
- is duplicated by
-
QTBUG-94155 Javascript-File-Import in inline component is null
-
- Closed
-
For Gerrit Dashboard: QTBUG-95095 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
362800,3 | QQmlObjectCreator: Add scripts also to context of inline components | dev | qt/qtdeclarative | Status: MERGED | +2 | 0 |
363595,2 | QQmlObjectCreator: Add scripts also to context of inline components | 6.2 | qt/qtdeclarative | Status: MERGED | +2 | 0 |
363597,1 | QQmlObjectCreator: Add scripts also to context of inline components | 6.1 | qt/qtdeclarative | Status: ABANDONED | 0 | 0 |