2017-04-12 63 views
1

我对ionic2/Angular2/Typescript是全新的。我正在创建一个有八个切片的轮子。我很难申报变量。我如何声明多个变量?如何在离子2/Angular2/typescript中声明多个变量?

在javascript中我已经声明象下面这样:

function rand(min, max) { 
    return Math.random() * (max - min) + min; 
} 

    var color = ['#fbc','#f88','#fbc','#f88','#fbc','#f88', "#fbc", "#f67"]; 
    var label = ['10', '200', '50', '100', '5', '500', '0', "jPOT"]; 
    var slices = color.length; 
    var sliceDeg = 360/slices; 
    var deg = rand(0, 360); 
    var speed = 0; 
    var slowDownRand = 0; 
    var ctx = canvas.getContext('2d'); 
    var width = canvas.width; // size 
    var center = width/2;  // center 
    var isStopped = false; 
    var lock = false; 

    function deg2rad(deg) { 
     return deg * Math.PI/180; 
    } 

如何申报ionic2?

+0

不知道离子但打字稿使用可以使用'var'太多,但有键入'let',你可以使用本地范围 –

回答

1

Ionic 2运行在Typescript之上,因此您可以按如下所示进行操作。

总是尝试使用let.You can read why here

rand(min, max): your-return-type { 
    return Math.random() * (max - min) + min; 
} 

    let color = ['#fbc','#f88','#fbc','#f88','#fbc','#f88', "#fbc", "#f67"], 
     label = ['10', '200', '50', '100', '5', '500', '0', "jPOT"], 
     slices = color.length, 
     sliceDeg = 360/slices, 
     deg = rand(0, 360), 
     speed = 0, 
     slowDownRand = 0, 
     ctx = canvas.getContext('2d'), 
     width = canvas.width, 
     center = width/2, 
     isStopped = false, 
     lock = false; 

    deg2rad(deg): your-return-type2 { 
     return deg * Math.PI/180; 
    } 
+1

你也可以用同样的'let'关键字来声明所有的人喜欢'让颜色= ...,标签= ...,幻灯片= ...,....' – sebaferreras

+0

感谢您的好提示@sebaferreras – Sampath

+1

不客气:) – sebaferreras

0

为什么要声明如此多的变量?如果变量太多,最好在TypeScript中创建一个类,然后在你的类中创建一个对象,最后将这些值赋给类属性。

class Wheel{ 
    public slices: string; 
    public speed: number; 
.... 
    } 

通过这种方式,您可以定义所有属性并在需要使用的地方使用它们。