一,TypeScript多行字符串java
傳統JavaScript字符串換行須要+進行拼接,而TypeScript不須要+拼接。看下面實例:spa
javaScript:blog
var content = "aaa"+ip
"bbbb";字符串
TypeScript:get
var content = `aaaio
bbb`;console
(注意這裏的``不是單引號,是雙撇號,鍵盤左上角的那個按鍵);function
二,TypeScript字符串模板模板
字符串模板就是在多行字符串裏用一個表達式插入變量,標籤,方法;
var myName = "zhang san";
var getName = function () {
return "zhang san";
}
//調用變量
console.log(`hello ${myName}`);
//插入標籤(這樣看起來比JavaScript好多了)
console.log(
`<div>
<span>${myName}</span>
<span>${getName}</span>
</div>`
)
//調用方法
console.log(`hello ${getName}`);
三,TypeScript自動拆分字符串
function test (template,name,age) {
console.log(template);
console.log(name);
console.log(age);
}
var myName = "zhang san";
var getAge = function () {
return 18;
}
test `hello my name is ${myName},i 'm ${getAge()}`;
(這裏name 就是 ${myName}, age 就是${getAge()})
輸入結果:
若是有用請支持,謝謝!