diff --git a/packages/uni-mp-compiler/__tests__/component.spec.ts b/packages/uni-mp-compiler/__tests__/component.spec.ts index ed4ca8af2e8cd227fbb4b4c32b40db870db79ff9..57e47549ea4cb87733e9b7a5934ac64b3e0654aa 100644 --- a/packages/uni-mp-compiler/__tests__/component.spec.ts +++ b/packages/uni-mp-compiler/__tests__/component.spec.ts @@ -101,10 +101,10 @@ describe('compiler: transform component', () => { }) test(`component with props`, () => { assert( - ``, - ``, + ``, + ``, `(_ctx, _cache) => { - return { a: _ctx.b, b: _ctx.d, c: _n(_ctx.f), d: _s(_ctx.h), e: _o(_ctx.i), f: _ctx.o, g: _ctx.r, h: _ctx.t, i: _o($event => _ctx.j = $event), j: _o($event => _ctx.k = $event), k: _p({ ['prop-a']: 'l', ['prop-b']: _ctx.m, first: _ctx.j, last: _ctx.k }) } + return { a: _ctx.id, b: _ctx.b, c: _ctx.d, d: _n(_ctx.f), e: _s(_ctx.h), f: _o(_ctx.i), g: _ctx.o, h: _ctx.r, i: _ctx.t, j: _o($event => _ctx.j = $event), k: _o($event => _ctx.k = $event), l: _p({ id: 'id', id: _ctx.id, ['prop-a']: 'l', ['prop-b']: _ctx.m, first: _ctx.j, last: _ctx.k }) } }` ) assert( diff --git a/packages/uni-mp-compiler/src/transforms/transformComponent.ts b/packages/uni-mp-compiler/src/transforms/transformComponent.ts index e10ea5f14436d53b61a24b74cf9b3e83416d7b0e..b832f4b14f7e4c48a3f9721c838ab32ad2c55597 100644 --- a/packages/uni-mp-compiler/src/transforms/transformComponent.ts +++ b/packages/uni-mp-compiler/src/transforms/transformComponent.ts @@ -116,7 +116,7 @@ function processBooleanAttr({ props }: ComponentNode) { } const builtInProps = [ - 'id', + // 'id', 'class', 'style', ATTR_VUE_ID, @@ -172,8 +172,10 @@ export function rewriteBinding( const properties: (ObjectProperty | SpreadElement)[] = [] for (let i = 0; i < props.length; i++) { const prop = props[i] + let isIdProp = false if (isAttributeNode(prop)) { const { name } = prop + isIdProp = name === 'id' if (!isComponentProp(name)) { continue } @@ -194,6 +196,7 @@ export function rewriteBinding( properties.push(spreadElement) } } else if (isStaticExp(arg)) { + isIdProp = arg.content === 'id' if (!isComponentProp(arg.content)) { continue } @@ -222,8 +225,11 @@ export function rewriteBinding( ) } } - props.splice(i, 1) - i-- + // 即保留 id 属性,又补充到 props 中 + if (!isIdProp) { + props.splice(i, 1) + i-- + } } if (properties.length) {