访问器运算符用于访问数组中的子资源、对象属性和元素。 还可以使用属性访问器来使用某些函数。
array[integerIndex]
object['stringIndex']
使用索引访问器从数组或对象中的属性获取元素。
对于 数组,请以 整数的形式提供索引。 整数匹配要检索的元素的从零开始的位置。
对于 对象,请以 字符串的形式提供索引。 字符串与要检索的对象的名称匹配。
以下示例获取数组中的元素。
var arrayVar = [
'Coho'
'Contoso'
'Fabrikam'
]
output accessorResult string = arrayVar[1]
示例中的输出:
名称 | 类型 | 价值 |
---|---|---|
访问器Result | 字符串 | “Contoso” |
下一个示例获取对象的属性。
var environmentSettings = {
dev: {
name: 'Development'
}
prod: {
name: 'Production'
}
}
output accessorResult string = environmentSettings['dev'].name
示例中的输出:
名称 | 类型 | 价值 |
---|---|---|
访问器Result | 字符串 | “开发” |
从 Bicep CLI 版本 0.34.x 开始,反向索引访问器运算符 (^
) 允许通过从末尾计数从数组中检索元素。 此基于单一的索引表示 ^1
返回最后一项、 ^2
第二到最后一项,依此类归。 索引必须是大于零的正整数,并且可以指定为文本或计算结果为整数的表达式。
array[^index]
如果索引超过数组的长度,则静态索引会发生编译错误,或者动态索引出现运行时错误。
对于常量数组,运算符在编译时计算。 对于动态输入(如 参数),在部署时进行评估。
var items = [
'apple'
'banana'
'orange'
'grape'
]
output secondToLast string = items[^2]
示例中的输出:
名称 | 类型 | 价值 |
---|---|---|
secondToLast | 字符串 | “orange” |
resourceName.functionName()
两个函数 - getSecret 和 list* - 支持访问器运算符来调用函数。 这两个函数是唯一支持访问器运算符的函数。
以下示例引用现有的密钥保管库,然后使用 getSecret
该保管库将机密传递给模块。
resource kv 'Microsoft.KeyVault/vaults@2023-07-01' existing = {
name: kvName
scope: resourceGroup(subscriptionId, kvResourceGroup )
}
module sql './sql.bicep' = {
name: 'deploySQL'
params: {
sqlServerName: sqlServerName
adminLogin: adminLogin
adminPassword: kv.getSecret('vmAdminPassword')
}
}
parentResource::nestedResource
嵌套资源是在另一个资源中声明的资源。 使用嵌套资源访问器 ::
从父资源外部访问该嵌套资源。
在父资源中,只需使用符号名称引用嵌套资源。 仅当从父资源外部引用嵌套资源时,才需要使用嵌套资源访问器。
以下示例演示如何从父资源和父资源外部引用嵌套资源。
resource demoParent 'demo.Rp/parentType@2024-01-01' = {
name: 'demoParent'
location: 'China North 2'
// Declare a nested resource within 'demoParent'
resource demoNested 'childType' = {
name: 'demoNested'
properties: {
displayName: 'The nested instance.'
}
}
// Declare another nested resource
resource demoSibling 'childType' = {
name: 'demoSibling'
properties: {
// Use symbolic name to reference because this line is within demoParent
displayName: 'Sibling of ${demoNested.properties.displayName}'
}
}
}
// Use nested accessor to reference because this line is outside of demoParent
output displayName string = demoParent::demoNested.properties.displayName
objectName.propertyName
使用属性访问器访问对象的属性。 属性访问器可与任何对象一起使用,包括对象的参数和变量。 对不是对象的表达式使用属性访问时,会出现错误。
下面的示例演示了一个对象变量以及如何访问属性。
var x = {
y: {
z: 'Hello'
a: true
}
q: 42
}
output outputZ string = x.y.z
output outputQ int = x.q
示例中的输出:
名称 | 类型 | 价值 |
---|---|---|
outputZ |
字符串 | “Hello” |
outputQ |
整数 | 42 |
通常,将属性访问器用于 Bicep 文件中部署的资源。 以下示例创建一个公共 IP 地址,并使用属性访问器从已部署的资源返回值。
resource publicIp 'Microsoft.Network/publicIPAddresses@2023-11-01' = {
name: publicIpResourceName
location: location
properties: {
publicIPAllocationMethod: dynamicAllocation ? 'Dynamic' : 'Static'
dnsSettings: {
domainNameLabel: publicIpDnsLabel
}
}
}
// Use property accessor to get value
output ipFqdn string = publicIp.properties.dnsSettings.fqdn
- 若要运行这些示例,请使用 Azure CLI 或 Azure PowerShell 来部署 Bicep 文件。
- 若要创建 Bicep 文件,请参阅快速入门:使用 Visual Studio Code 创建 Bicep 文件。
- 有关如何解决 Bicep 类型错误的信息,请参阅 Bicep 的 Any 函数。