名字解析(作用域)
当你使用不带元素前缀的名字时,Slint 会先在当前元素上查找它,
然后在其父元素上查找,如此一直向上直到组件的根元素。
当你用元素(或 self、parent、root)限定名字时,
只会在该元素上搜索。
这意味着一个函数可以遮蔽祖先元素上的另一个函数:
export component Example { property <int> secret_number: my-function(); public pure function my-function() -> int { return 1; }
VerticalLayout { public pure function my-function() -> int { return 2; }
Text { text: "The secret number is " + my-function(); public pure function my-function() -> int { return 3; } }
Text { text: "The other secret number is " + my-function(); } }}slint
在这个示例中,secret_number 是 1,
两个标签分别显示 “The secret number is 3” 和 “The other secret number is 2”。
确切的规则见语言参考中的名字解析 章节。
© 2026 SixtyFPS GmbH