mirror of https://github.com/vuejs/core.git
feat(compiler-vapor): support v-for without prefixIdentifiers (#259)
Co-authored-by: 三咲智子 Kevin Deng <sxzz@sxzz.moe>
This commit is contained in:
parent
5eb43b08b2
commit
b44ca85cb1
|
@ -30,6 +30,20 @@ export function render(_ctx) {
|
|||
}"
|
||||
`;
|
||||
|
||||
exports[`compiler: v-for > function params w/ prefixIdentifiers: false 1`] = `
|
||||
"import { renderEffect as _renderEffect, setText as _setText, createFor as _createFor, template as _template } from 'vue/vapor';
|
||||
const t0 = _template("<div></div>")
|
||||
|
||||
export function render(_ctx) {
|
||||
const n0 = _createFor(() => (items), ([item, __, k]) => {
|
||||
const n2 = t0()
|
||||
_renderEffect(() => _setText(n2, item))
|
||||
return n2
|
||||
}, (item, __, k) => (k))
|
||||
return n0
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`compiler: v-for > multi effect 1`] = `
|
||||
"import { renderEffect as _renderEffect, setDynamicProp as _setDynamicProp, createFor as _createFor, template as _template } from 'vue/vapor';
|
||||
const t0 = _template("<div></div>")
|
||||
|
|
|
@ -223,4 +223,17 @@ describe('compiler: v-for', () => {
|
|||
index: undefined,
|
||||
})
|
||||
})
|
||||
|
||||
test('function params w/ prefixIdentifiers: false', () => {
|
||||
const { code } = compileWithVFor(
|
||||
`<div v-for="(item, , k) of items" :key="k">{{ item }}</div>`,
|
||||
{
|
||||
prefixIdentifiers: false,
|
||||
},
|
||||
)
|
||||
|
||||
expect(code).contains(`_createFor(() => (items), ([item, __, k]) => {`)
|
||||
expect(code).contain(`_setText(n2, item)`)
|
||||
expect(code).matchSnapshot()
|
||||
})
|
||||
})
|
||||
|
|
|
@ -42,13 +42,18 @@ export function genFor(
|
|||
}
|
||||
|
||||
const [depth, exitScope] = context.enterScope()
|
||||
const propsName = `_ctx${depth}`
|
||||
let propsName: string
|
||||
const idMap: Record<string, string | null> = {}
|
||||
if (context.options.prefixIdentifiers) {
|
||||
propsName = `_ctx${depth}`
|
||||
Array.from(idsOfValue).forEach(
|
||||
(id, idIndex) => (idMap[id] = `${propsName}[${idIndex}]`),
|
||||
)
|
||||
if (rawKey) idMap[rawKey] = `${propsName}[${idsOfValue.size}]`
|
||||
if (rawIndex) idMap[rawIndex] = `${propsName}[${idsOfValue.size + 1}]`
|
||||
} else {
|
||||
propsName = `[${[rawValue || ((rawKey || rawIndex) && '_'), rawKey || (rawIndex && '__'), rawIndex].filter(Boolean).join(', ')}]`
|
||||
}
|
||||
|
||||
let blockFn = context.withId(
|
||||
() => genBlock(render, context, [propsName]),
|
||||
|
|
Loading…
Reference in New Issue