提交 762dca60 编写于 作者: fxy060608's avatar fxy060608

fix(mp-weixin): event key (#4015)

上级 6972d064
......@@ -63,6 +63,7 @@ export type VForOptions = Omit<ForParseResult, 'tagType'> & {
indexCode: string
indexExpr: Identifier | Pattern | RestElement
indexAlias: string
node: ElementNode
}
export type ForElementNode = ElementNode & {
......@@ -165,6 +166,7 @@ export const transformFor = createStructuralDirectiveTransform(
indexCode,
indexExpr,
indexAlias,
node,
}
const vForScope = context.addVForScope({
......@@ -237,7 +239,7 @@ function parseAlias(
return fallback
}
function parseVForScope(currentScope: CodegenScope) {
export function parseVForScope(currentScope: CodegenScope) {
while (currentScope) {
if (isVForScope(currentScope) && !isScopedSlotVFor(currentScope)) {
return currentScope
......
......@@ -23,6 +23,7 @@ import { V_ON } from '../runtimeHelpers'
import { TransformContext } from '../transform'
import { DirectiveTransformResult } from './transformElement'
import { isBuiltInIdentifier, processExpression } from './transformExpression'
import { parseVForScope } from './vFor'
const fnExpRE =
/^\s*([\w$_]+|(async\s*)?\([^)]*?\))\s*=>|^\s*(async\s+)?function(?:\s+[\w$]+)?\s*\(/
......@@ -212,7 +213,13 @@ export function wrapperVOn(
}
const keys: string[] = []
if (context.miniProgram.event?.key && context.inVFor) {
const keyProp = findProp(node, 'key')
let keyProp = findProp(node, 'key')
if (!keyProp) {
const vForScope = parseVForScope(context.currentScope)
if (vForScope) {
keyProp = findProp(vForScope.node, 'key')
}
}
// 对 for 中的所有事件增加 key 标记,避免微信小程序不更新事件对象
if (keyProp && isDirectiveNode(keyProp) && keyProp.exp) {
const keyCode = genExpr(keyProp.exp)
......
......@@ -23,6 +23,13 @@ describe('mp-weixin: transform v-on', () => {
`<view wx:for="{{a}}" wx:for-item="item" wx:key="b" bindtap="{{item.c}}">{{item.a}}</view>`,
`(_ctx, _cache) => {
return { a: _f(_ctx.arr, (item, k0, i0) => { return { a: _t(item.text), b: item._id, c: _o($event => _ctx.show(item), item._id) }; }) }
}`
)
assert(
`<view v-for="(item, index) in list" :key="item.id"><view @click="handleClickItem(item, index)"></view></view>`,
`<view wx:for="{{a}}" wx:for-item="item" wx:key="b"><view bindtap="{{item.a}}"></view></view>`,
`(_ctx, _cache) => {
return { a: _f(_ctx.list, (item, index, i0) => { return { a: _o($event => _ctx.handleClickItem(item, index), item.id), b: item.id }; }) }
}`
)
assert(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册