1,qml中加入JavaScript时,JavaScript必须放入到调用的同一个组件中,如下例子:
import QtQuick 1.1
//import "factorial.js" as Fac
Rectangle
{
id:aa
color:"red"
x:10
y:10
width:100
height:100
function fun(a)
{
a=parseInt(a);
if(a<=0) return 1;
else return a*fun(a-1);
}
MouseArea
{
anchors.fill:parent
onClicked:console.log(fun(4))
//parent.color="blue"
//console.log("kklll;")
}
}
2,import “factorial.js” as Fac ;as的对象必须第一个字母大写,否则出现无法识别的标识符。
import QtQuick 1.1
import "factorial.js" as Fac
Rectangle
{
id:aa
color:"red"
x:10
y:10
width:100
height:100
MouseArea
{
anchors.fill:parent
onClicked:console.log(Fac.fun(4))
//parent.color="blue"
//console.log("kklll;")
}
}
factorial.js如下:
function fun(a) { a=parseInt(a); if(a<=0) return 1; else return a*fun(a-1); }