在QML中加载页面时运行javascript函数

2 8*_*2 8 16 javascript qt qml

我想像这样运行js函数

function setPersonalInfoValue(){
        console.debug("setPersonalInfoValue()");
    }
Run Code Online (Sandbox Code Playgroud)

当页面加载QML时.

我试过这个:

Rectangle {
    id: page2
    width: 100
    height: 62

    function setPersonalInfoValue(){
        console.debug("setPersonalInfoValue()");
    }
}
Run Code Online (Sandbox Code Playgroud)

我想在第1页到第2页之间切换时运行setPersonalInfoValue()函数.我该怎么做?

2 8*_*2 8 25

最后,我使用了:

Component.onCompleted: {
    setPersonalInfoValue();
}
Run Code Online (Sandbox Code Playgroud)


Dea*_*ron 6

这个问题的答案取决于您切换页面的精确程度。如果您要动态加载页面,则可以使用 Component.onCompleted。这将在 QML 完成加载对象时执行。如果您使用其他方法,例如更改可见性以显示不同的页面,那么您应该依赖该属性的 onChanged 信号。