如何在打字稿中的多行中返回一个字符串

时间:2017-12-16 06:11:12

标签: javascript typescript

我想在多行中返回变量的值。

page.ts

//all imports are done

export class page {

let title1: string;
let heading: string;
let heading2: string;
let heading4 : string;

constructor(){}

public onClick(){   //from html I am calling this onClick()
   return testFunction(); 
}

public testFunction(){
return this.title1 + '\ '+ this.heading + '\ '+ this.heading2 + '\ ' + this.heading4;
}
}

// display - >

ABCD Today is very cold here What about there?
How are you?

预期输出 - >

                    ABCD

                    Today is very cold here

                    What about there?

                    How are you?

AS title1 = ABCD

heading1 =今天天气很冷。

heading2 =那里怎么样?

heading4 =你好吗?

1 个答案:

答案 0 :(得分:3)

您可以使用Typescript中也支持的ES6 template literals。您可以自己做的其余调整

public testFunction(){
     return `${this.title1}
             ${this.heading}
             ${this.heading2}
             ${this.heading4}`;
}