-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1405 from didi/feat-support-option-chain-simple
feat: add wxml option chain
- Loading branch information
Showing
4 changed files
with
223 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# 模版内可选链表达式 | ||
|
||
Mpx提供了在模版中使用可选链`?.`访问变量属性的能力,用法/能力和JS的可选链基本一致,可以在任意被`{{}}`所包裹的模版语法内使用。 | ||
> 实现原理是在编译时将使用`?.`的部分转化为`wxs`函数调用,运行时通过`wxs`访问变量属性,模拟出可选链的效果。 | ||
使用示例: | ||
```html | ||
<template> | ||
<view wx:if="{{ a?.b }}">{{ a?.c + a?.d }}</view> | ||
<view wx:for="{{ a?.d || [] }}"></view> | ||
<view>{{ a?.d ? 'a' : 'b' }}</view> | ||
<view>{{ a?.g[e?.d] }}</view> | ||
</template> | ||
|
||
<script> | ||
import { createComponent } from '@mpxjs/core' | ||
createComponent({ | ||
data: { | ||
a: { | ||
b: true, | ||
c: 123, | ||
g: { | ||
d: 321 | ||
} | ||
}, | ||
e: { | ||
d: 'd' | ||
} | ||
} | ||
}) | ||
</script> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// 可选链wxs | ||
module.exports.g = function (val, valKeyArr) { | ||
var res = val | ||
if (typeof val !== 'object') { | ||
return undefined | ||
} | ||
var len = valKeyArr.length | ||
var i = 0 | ||
while (i < len) { | ||
if ((res = res[valKeyArr[i]]) === undefined) { | ||
break | ||
} | ||
i++ | ||
} | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters