Component
8 methods@Component 装饰器及相关 API,用于定义组件、输入/输出和生命周期钩子。
[ -f file ]将类标记为 Angular 组件并附加元数据(selector、template、styles)的装饰器。
Parameters
| Name | Type | Description |
|---|---|---|
| file | path | Path to test. |
Returns
boolean (exit status)
Example
bash
if [ -f /etc/hosts ]; then
echo "regular file exists"
fi[ -d dir ]将类属性标记为来自父组件模板的数据绑定输入。
Parameters
| Name | Type | Description |
|---|---|---|
| dir | path | Path to test. |
Returns
boolean (exit status)
Example
bash
if [ -d /tmp ]; then
echo "directory exists"
fi[ -z string ]将属性标记为输出,父组件可通过事件绑定进行绑定。
Parameters
| Name | Type | Description |
|---|---|---|
| string | string | String to test. |
Returns
boolean (exit status)
Example
bash
name=""
if [ -z "$name" ]; then
echo "name is empty"
fi[ -n string ]在 Angular 初始化指令的数据 绑定属性后调用一次的生命周期钩子。
Parameters
| Name | Type | Description |
|---|---|---|
| string | string | String to test. |
Returns
boolean (exit status)
Example
bash
name="hello"
if [ -n "$name" ]; then
echo "name is set"
fi[ str1 = str2 ]在 Angular 销毁指令前调用一次的生命周期钩子。用于清理。
Parameters
| Name | Type | Description |
|---|---|---|
| str1 | string | First string. |
| str2 | string | Second string. |
Returns
boolean (exit status)
Example
bash
if [ "$USER" = "root" ]; then
echo "running as root"
fi
if [ "$a" != "$b" ]; then
echo "different"
fi[ num1 -eq num2 ]Returns true if the two integers are equal. Other numeric ops: -ne, -lt, -le, -gt, -ge.
Parameters
| Name | Type | Description |
|---|---|---|
| num1 | integer | First integer. |
| num2 | integer | Second integer. |
Returns
boolean (exit status)
Example
bash
count=5
if [ $count -eq 5 ]; then echo "five"; fi
if [ $count -gt 3 ]; then echo "greater than 3"; fi
if [ $count -le 5 ]; then echo "less or equal 5"; fi[ -e path ]Returns true if path exists (any type: file, directory, symlink, etc.).
Parameters
| Name | Type | Description |
|---|---|---|
| path | path | Path to test. |
Returns
boolean (exit status)
Example
bash
if [ -e /usr/bin/python3 ]; then
echo "python3 exists"
fi[[ str =~ regex ]]Returns true if str matches the extended regular expression regex. Only in [[ ]], not [ ].
Parameters
| Name | Type | Description |
|---|---|---|
| str | string | String to test. |
| regex | regex | Extended regular expression. |
Returns
boolean (exit status)
Example
bash
phone="123-456-7890"
if [[ $phone =~ ^[0-9]{3}-[0-9]{3}-[0-9]{4}$ ]]; then
echo "valid phone"
fi
# Capture groups: ${BASH_REMATCH[1]}, etc.