Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-95095

Inline QML component can't use named import of JavaScript module from enclosing scope

    XMLWordPrintable

    Details

    • Platform/s:
      All
    • Commits:
      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

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            Activity

              People

              Assignee:
              qtqmlteam Qt Qml Team User
              Reporter:
              ratijas ivan tkachenko
              Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:

                  Gerrit Reviews

                  There are no open Gerrit changes