在声明前使用 var
qmllint 将此警告类别拼写为[var-used-before-declaration] 。
此处使用了尚未声明的变量
发生了什么?
您在声明变量之前就使用了该变量。
为什么这样不好?
这会降低代码的可读性。使用let 或const 声明的变量会在运行时报错。
示例
import QtQuick
Item {
function f() {
x = 42; // x is used before its declaration
let x;
}
Component.onCompleted: f()
}要解决此警告,请将变量声明移至使用位置之前:
import QtQuick
Item {
function f() {
let x;
x = 42;
}
Component.onCompleted: f()
}另请参阅 《QML 文档中的 JavaScript 表达式》。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.