提交 06fcd97d 编写于 作者: fxy060608's avatar fxy060608

fix(v3): dynamic ref #1458

上级 48ec6ae4
...@@ -153,5 +153,12 @@ describe('codegen', () => { ...@@ -153,5 +153,12 @@ describe('codegen', () => {
`with(this){return _c('base-layout',{attrs:{"_i":0},scopedSlots:_u([{key:_$s(1,'st',dynamicSlotName),fn:function(_empty_, _svm, _si){return undefined}}],null,true)})}` `with(this){return _c('base-layout',{attrs:{"_i":0},scopedSlots:_u([{key:_$s(1,'st',dynamicSlotName),fn:function(_empty_, _svm, _si){return undefined}}],null,true)})}`
) )
}) })
it('generate ref', () => {
assertCodegen(
'<p :ref="component1"></p>',
`with(this){return _c('p',{ref:_$s(0,'ref',component1)})}`
)
})
}) })
/* eslint-enable quotes */ /* eslint-enable quotes */
...@@ -93,6 +93,12 @@ describe('codegen', () => { ...@@ -93,6 +93,12 @@ describe('codegen', () => {
'<base-layout><template v-slot:[dynamicSlotName]></template></base-layout>', '<base-layout><template v-slot:[dynamicSlotName]></template></base-layout>',
`with(this){return _c('base-layout',{attrs:{"_i":0},scopedSlots:_u([{key:_$g(1,'st'),fn:function(_empty_, _svm, _si){return undefined}}],null,true)})}` `with(this){return _c('base-layout',{attrs:{"_i":0},scopedSlots:_u([{key:_$g(1,'st'),fn:function(_empty_, _svm, _si){return undefined}}],null,true)})}`
) )
})
it('generate ref', () => {
assertCodegen(
'<p :ref="component1"></p>',
`with(this){return _c('p',{ref:_$g(0,'ref'),attrs:{"_i":0}})}`
)
}) })
}) })
/* eslint-enable quotes */ /* eslint-enable quotes */
const { const {
ID, ID,
C_IS, C_IS,
C_REF,
V_IF, V_IF,
V_FOR, V_FOR,
V_ELSE_IF, V_ELSE_IF,
...@@ -9,6 +10,12 @@ const { ...@@ -9,6 +10,12 @@ const {
const parseTextExpr = require('./text-parser') const parseTextExpr = require('./text-parser')
function parseRef (el, genVar) {
if (el.ref && isVar(el.ref)) {
el.ref = genVar(C_REF, el.ref)
}
}
function parseIs (el, genVar) { function parseIs (el, genVar) {
if (!el.component) { if (!el.component) {
return return
...@@ -97,7 +104,8 @@ function parseText (el, parent, state) { ...@@ -97,7 +104,8 @@ function parseText (el, parent, state) {
} }
module.exports = { module.exports = {
parseIs, parseIs,
parseRef,
parseIf, parseIf,
parseFor, parseFor,
parseText, parseText,
......
...@@ -17,6 +17,7 @@ const { ...@@ -17,6 +17,7 @@ const {
const { const {
parseIs, parseIs,
parseRef,
parseIf, parseIf,
parseFor, parseFor,
parseText, parseText,
...@@ -130,6 +131,7 @@ function transformNode (el, parent, state, isScopedSlot) { ...@@ -130,6 +131,7 @@ function transformNode (el, parent, state, isScopedSlot) {
const genVar = createGenVar(el.attrsMap[ID], isScopedSlot) const genVar = createGenVar(el.attrsMap[ID], isScopedSlot)
parseIs(el, genVar) parseIs(el, genVar)
parseRef(el, genVar)
parseFor(el, createGenVar, isScopedSlot, checkAutoFill(el)) parseFor(el, createGenVar, isScopedSlot, checkAutoFill(el))
parseKey(el, isScopedSlot) parseKey(el, isScopedSlot)
...@@ -162,10 +164,10 @@ function postTransformNode (el, options) { ...@@ -162,10 +164,10 @@ function postTransformNode (el, options) {
} else { } else {
options.root = el options.root = el
} }
traverseNode(el, false, { traverseNode(el, false, {
createGenVar, createGenVar,
forIteratorId: 0, forIteratorId: 0,
transformNode, transformNode,
filterModules: options.filterModules filterModules: options.filterModules
}) })
optimize(el, options) optimize(el, options)
......
...@@ -13,6 +13,8 @@ const SET_MP_CLASS = '_$smc' ...@@ -13,6 +13,8 @@ const SET_MP_CLASS = '_$smc'
const GET_CHANGE_DATA = '_$gc' // wxs const GET_CHANGE_DATA = '_$gc' // wxs
const C_IS = 'is' const C_IS = 'is'
const C_SLOT_TARGET = 'st'
const C_REF = 'ref'
const V_FOR = 'f' const V_FOR = 'f'
const V_IF = 'i' const V_IF = 'i'
...@@ -208,7 +210,7 @@ function traverseNode (el, parent, state, isScopedSlot) { ...@@ -208,7 +210,7 @@ function traverseNode (el, parent, state, isScopedSlot) {
state.childIndex = index state.childIndex = index
slot.slotScope = `${slot.slotScope}, _svm, _si` slot.slotScope = `${slot.slotScope}, _svm, _si`
if (slot.slotTargetDynamic && slot.slotTarget) { if (slot.slotTargetDynamic && slot.slotTarget) {
slot.slotTarget = state.createGenVar(slot.attrsMap[ID])('st', slot.slotTarget) slot.slotTarget = state.createGenVar(slot.attrsMap[ID])(C_SLOT_TARGET, slot.slotTarget)
} }
traverseNode(slot, el, state, true) traverseNode(slot, el, state, true)
}) })
...@@ -256,6 +258,7 @@ function addHandler (el, name, value, important) { ...@@ -256,6 +258,7 @@ function addHandler (el, name, value, important) {
module.exports = { module.exports = {
C_IS, C_IS,
C_REF,
V_FOR, V_FOR,
V_IF, V_IF,
V_ELSE_IF, V_ELSE_IF,
......
...@@ -11,7 +11,8 @@ const { ...@@ -11,7 +11,8 @@ const {
} = require('./util') } = require('./util')
const { const {
parseIs, parseIs,
parseRef,
parseIf, parseIf,
parseFor, parseFor,
parseText, parseText,
...@@ -126,7 +127,7 @@ function transformNode (el, parent, state, isScopedSlot) { ...@@ -126,7 +127,7 @@ function transformNode (el, parent, state, isScopedSlot) {
const genVar = createGenVar(el.attrsMap[ID], isScopedSlot) const genVar = createGenVar(el.attrsMap[ID], isScopedSlot)
parseIs(el, genVar) parseIs(el, genVar)
parseRef(el, genVar)
if (parseFor(el, createGenVar, isScopedSlot)) { if (parseFor(el, createGenVar, isScopedSlot)) {
if (el.alias[0] === '{') { // <div><li v-for=" { a, b } in items"></li></div> if (el.alias[0] === '{') { // <div><li v-for=" { a, b } in items"></li></div>
el.alias = '$item' el.alias = '$item'
...@@ -161,7 +162,7 @@ function postTransformNode (el, options) { ...@@ -161,7 +162,7 @@ function postTransformNode (el, options) {
} else { } else {
options.root = el options.root = el
} }
traverseNode(el, false, { traverseNode(el, false, {
createGenVar, createGenVar,
forIteratorId: 0, forIteratorId: 0,
transformNode, transformNode,
...@@ -242,4 +243,4 @@ module.exports = { ...@@ -242,4 +243,4 @@ module.exports = {
}, },
postTransformNode, postTransformNode,
genData genData
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册