【angular2】@angula/cdk/coercion 学习01

1. cdk 文档 https://material.angular.io/cdk/categories

2. coercion 模块提供了一些类型转换的工具。

  • import {
        _isNumberValue,
        coerceArray,
        coerceBooleanProperty,
        coerceCssPixelValue,
        coerceNumberProperty
    } from "@angular/cdk/coercion";

(1)转换为 number

  • coerceNumberProperty(10.5); // 10.5
    
    console.log("是否是number类型: " + coerceNumberProperty('a', ()=>'不是')); // 第二参数是转换为 number 时的回调
    console.log("是否是number类型: " + _isNumberValue('a'));

(2)转换为 boolean

  • coerceBooleanProperty('false'); // false

(3)转换为 array

  • coerceArray(1204) // [1024]

(4)转换为 CSS pixel value

  • coerceCssPixelValue('10'); // '10px'

(6)转换为 string array

  • coerceStringArray(null); // []
    // null ---> []
    // [null] ---> ["null"]
    // ["a", "b ", " "] ---> ["a", "b"]
    // [1, [2, 3]] ---> ["1", "2,3"]
    // [{ a: 0 }] ---> ["[object Object]"]
    // { a: 0 } ---> ["[object", "Object]"]

(7)转换为 coerceElement

  • coerceElement(1); // 1

 

posted @ 2021-02-19 11:48  耶梦加德  阅读(123)  评论(0编辑  收藏  举报