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

feat(v3): v-slot

上级 51f5cc39
......@@ -63,6 +63,13 @@ describe('codegen', () => {
'<div>A{{ d | e | f }}B{{text}}C</div>',
`with(this){return _c('div',[_v((_$s(0,'t0',_s(_f("f")(_f("e")(d)))))+(_$s(0,'t1',_s(text))))])}`
)
})
it('generate v-slot', () => {
assertCodegen(
'<current-user v-slot="{ user }">{{ user.firstName }}</current-user>',
`with(this){return _c('current-user',{attrs:{"_i":0},scopedSlots:_u([{key:"default",fn:function({ user }){return [_v((_$s(0,'t0',_s(user.firstName))))]}}])})}`
)
})
})
/* eslint-enable quotes */
......@@ -39,6 +39,12 @@ describe('codegen', () => {
'<div><slot><div>{{hi}}</div></slot></div>',
`with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_t("default",[_c('v-uni-view',{attrs:{"_i":2}},[_v((_$g(2,'t0')))])],{"_i":1})],2)}`
)
})
it('generate v-slot', () => {
assertCodegen(
'<current-user v-slot="{ user }">{{ user.firstName }}</current-user>',
`with(this){return _c('current-user',{attrs:{"_i":0},scopedSlots:_u([{key:"default",fn:function({ user }){return [_v((_$g(0,'t0')))]}}])})}`
)
})
})
/* eslint-enable quotes */
......@@ -565,7 +565,7 @@ describe('codegen', () => {
it('generate svg component with children', () => {
assertCodegen(
'<svg><my-comp><circle :r="10"></circle></my-comp></svg>',
`with(this){return _c('svg',{attrs:{"_i":0}},[_c('my-comp',{attrs:{"_i":1}},[_c('circle',{attrs:{"_i":2}})])],1)}`
`with(this){return _c('svg',{attrs:{"_i":0}},[_c('my-comp',{attrs:{"_i":1}},[_c('circle',{attrs:{"_i":2}})],1)],1)}`
)
})
......
const compiler = require('../lib')
const res = compiler.compile(
`
<input v-model="\n test \n">
`, {
<div><block v-for="item in items"><div></div><div></div></block></div>
`, {
resourcePath: '/User/fxy/Documents/test.wxml',
isReservedTag: function (tag) {
return true
......@@ -15,7 +13,7 @@ const res = compiler.compile(
mp: {
platform: 'app-plus'
},
// service: true,
service: true,
view: true
})
console.log(require('util').inspect(res, {
......
module.exports = function parseBlock (el) {
if (el.tag === 'block') {
const {
ID,
hasOwn,
addRawAttr
} = require('../util')
module.exports = function parseBlock (el, parent) {
if (el.tag === 'template' && !hasOwn(el.attrsMap, ID)) {
/**
* <current-user v-slot="{ user }">
* {{ user.firstName }}
* </current-user>
*/
addRawAttr(el, ID, parent.attrsMap[ID])
} else if (el.tag === 'block') {
el.tag = 'template'
const vForKey = el.key
const vForKey = el.key
if (vForKey) {
delete el.key
el.children.forEach((childEl, index) => {
......
......@@ -10,6 +10,6 @@ module.exports = function parseComponent (el) {
// 需要把自定义组件的 attrs, props 全干掉
if (el.tag && !hasOwn(tags, el.tag.replace('v-uni-', ''))) {
// 仅保留 ID
el.attrs = el.attrs.filter(attr => attr.name === ID)
el.attrs && (el.attrs = el.attrs.filter(attr => attr.name === ID))
}
}
......@@ -66,7 +66,7 @@ function transformNode (el, parent, state) {
if (el.type === 3) {
return
}
parseBlock(el)
parseBlock(el, parent)
parseEvent(el)
updateForEleId(el, state)
......@@ -115,7 +115,7 @@ function genVModel (el) {
const prop = el.props.find(prop => prop.name === 'value')
prop.value = createGenVar(el.attrsMap[ID])('v-model', prop.value)
}
if (el.model) {
if (el.model) {
el.model.value = createGenVar(el.attrsMap[ID])('v-model', el.model.value)
}
}
......
......@@ -57,7 +57,7 @@ function transformNode (el, parent, state) {
if (el.type === 3) {
return
}
parseBlock(el)
parseBlock(el, parent)
parseComponent(el)
parseEvent(el)
// 更新 id
......
......@@ -43,7 +43,8 @@ module.exports = {
return compiled
} else if (options.view) {
(options.modules || (options.modules = [])).push(require('./app/view'))
options.optimize = false // 暂不启用 staticRenderFns
options.optimize = false // 暂不启用 staticRenderFns
options.isReservedTag = (tagName) => false // 均为组件
return compile(source, options)
}
......
......@@ -185,7 +185,7 @@ const {
function isComponent (tagName) {
if (tagName === 'block' || tagName === 'template') {
return false
}
}
return !hasOwn(tags, getTagName(tagName.replace('v-uni-', '')))
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册