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

feat(v3): v-slot

上级 51f5cc39
...@@ -63,6 +63,13 @@ describe('codegen', () => { ...@@ -63,6 +63,13 @@ describe('codegen', () => {
'<div>A{{ d | e | f }}B{{text}}C</div>', '<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))))])}` `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 */ /* eslint-enable quotes */
...@@ -39,6 +39,12 @@ describe('codegen', () => { ...@@ -39,6 +39,12 @@ describe('codegen', () => {
'<div><slot><div>{{hi}}</div></slot></div>', '<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)}` `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 */ /* eslint-enable quotes */
...@@ -565,7 +565,7 @@ describe('codegen', () => { ...@@ -565,7 +565,7 @@ describe('codegen', () => {
it('generate svg component with children', () => { it('generate svg component with children', () => {
assertCodegen( assertCodegen(
'<svg><my-comp><circle :r="10"></circle></my-comp></svg>', '<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 compiler = require('../lib')
const res = compiler.compile( 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', resourcePath: '/User/fxy/Documents/test.wxml',
isReservedTag: function (tag) { isReservedTag: function (tag) {
return true return true
...@@ -15,7 +13,7 @@ const res = compiler.compile( ...@@ -15,7 +13,7 @@ const res = compiler.compile(
mp: { mp: {
platform: 'app-plus' platform: 'app-plus'
}, },
// service: true, service: true,
view: true view: true
}) })
console.log(require('util').inspect(res, { console.log(require('util').inspect(res, {
......
module.exports = function parseBlock (el) { const {
if (el.tag === 'block') { 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' el.tag = 'template'
const vForKey = el.key const vForKey = el.key
if (vForKey) { if (vForKey) {
delete el.key delete el.key
el.children.forEach((childEl, index) => { el.children.forEach((childEl, index) => {
......
...@@ -10,6 +10,6 @@ module.exports = function parseComponent (el) { ...@@ -10,6 +10,6 @@ module.exports = function parseComponent (el) {
// 需要把自定义组件的 attrs, props 全干掉 // 需要把自定义组件的 attrs, props 全干掉
if (el.tag && !hasOwn(tags, el.tag.replace('v-uni-', ''))) { if (el.tag && !hasOwn(tags, el.tag.replace('v-uni-', ''))) {
// 仅保留 ID // 仅保留 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) { ...@@ -66,7 +66,7 @@ function transformNode (el, parent, state) {
if (el.type === 3) { if (el.type === 3) {
return return
} }
parseBlock(el) parseBlock(el, parent)
parseEvent(el) parseEvent(el)
updateForEleId(el, state) updateForEleId(el, state)
...@@ -115,7 +115,7 @@ function genVModel (el) { ...@@ -115,7 +115,7 @@ function genVModel (el) {
const prop = el.props.find(prop => prop.name === 'value') const prop = el.props.find(prop => prop.name === 'value')
prop.value = createGenVar(el.attrsMap[ID])('v-model', prop.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) el.model.value = createGenVar(el.attrsMap[ID])('v-model', el.model.value)
} }
} }
......
...@@ -57,7 +57,7 @@ function transformNode (el, parent, state) { ...@@ -57,7 +57,7 @@ function transformNode (el, parent, state) {
if (el.type === 3) { if (el.type === 3) {
return return
} }
parseBlock(el) parseBlock(el, parent)
parseComponent(el) parseComponent(el)
parseEvent(el) parseEvent(el)
// 更新 id // 更新 id
......
...@@ -43,7 +43,8 @@ module.exports = { ...@@ -43,7 +43,8 @@ module.exports = {
return compiled return compiled
} else if (options.view) { } else if (options.view) {
(options.modules || (options.modules = [])).push(require('./app/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) return compile(source, options)
} }
......
...@@ -185,7 +185,7 @@ const { ...@@ -185,7 +185,7 @@ const {
function isComponent (tagName) { function isComponent (tagName) {
if (tagName === 'block' || tagName === 'template') { if (tagName === 'block' || tagName === 'template') {
return false return false
} }
return !hasOwn(tags, getTagName(tagName.replace('v-uni-', ''))) 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.
先完成此消息的编辑!
想要评论请 注册