0
点赞
收藏
分享

微信扫一扫

ES6 拼接字符串(angular)


angular 拼接字符串有没有什么好办法呢,发现es6可以。

拼接方式:用反引号( ` )包裹起来

特点:

  1. 模板中的内容可以有格式并可以定义多行
  2. 通过${}方式填充数据
  3. 大括号里面可以进行运算和调用函数

例子: 

// 例1
const content1 = 'hello boys!';
this.message = `hello world! ${content1}`;

ES6 拼接字符串(angular)_ES6 拼接字符串

// 例2
const content1 = 'hello boys!';
this.message = `hello world! ${content1}
  <p style="color">hello girls!</p>`;

ES6 拼接字符串(angular)_调用函数_02

// 例3
this.message = `2 + 5 = ${2 + 5}`;

ES6 拼接字符串(angular)_angular_03

// 例4
this.message = `3 + 6 = ${this.sum(3, 6)}`;
sum (a: number, b:number):number {
  return a + b;
}

ES6 拼接字符串(angular)_大括号_04

举报

相关推荐

0 条评论