From 13e58408a32af4ed4747942c010ae39b4eedbe1d Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Sat, 19 Oct 2019 17:28:19 +0800 Subject: [PATCH] feat(v3): improve slot logic --- .../compiler-app-plus-extra.view.spec.js | 6 ++++ .../uni-template-compiler/__tests__/demo.js | 2 +- .../uni-template-compiler/lib/app/view.js | 30 +++++++++++++------ .../plugins/\346\270\262\346\237\223.md" | 25 ---------------- 4 files changed, 28 insertions(+), 35 deletions(-) delete mode 100644 "src/platforms/app-plus/view/framework/plugins/\346\270\262\346\237\223.md" diff --git a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js index 62278524d..64a623d1f 100644 --- a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js @@ -33,6 +33,12 @@ describe('codegen', () => { '
A{{ d | e | f }}B{{text}}C
', `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v("A"+(_$g(0,'t0'))+"B"+(_$g(0,'t1'))+"C")])}` ) + }) + it('generate slot fallback content', () => { + assertCodegen( + '
{{hi}}
', + `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_t("default",[_c('v-uni-view',{attrs:{"_i":2}},[_v((_$g(2,'t0',1)))])],{"_i":1})],2)}` + ) }) }) /* eslint-enable quotes */ diff --git a/packages/uni-template-compiler/__tests__/demo.js b/packages/uni-template-compiler/__tests__/demo.js index e4bea3e85..2ac63bdad 100644 --- a/packages/uni-template-compiler/__tests__/demo.js +++ b/packages/uni-template-compiler/__tests__/demo.js @@ -1,7 +1,7 @@ const compiler = require('../lib') const res = compiler.compile( ` -
+
{{hi}}
`, { resourcePath: '/User/fxy/Documents/test.wxml', isReservedTag: function (tag) { diff --git a/packages/uni-template-compiler/lib/app/view.js b/packages/uni-template-compiler/lib/app/view.js index 08b451f73..5fce3ea1c 100644 --- a/packages/uni-template-compiler/lib/app/view.js +++ b/packages/uni-template-compiler/lib/app/view.js @@ -16,23 +16,35 @@ const parseComponent = require('./parser/component-parser') const basePreTransformNode = require('./pre-transform-node') -function createGenVar (id) { +function createGenVar (id, isInSlot = false) { return function genVar (name, extra = '') { + const isFallbackContent = isInSlot ? ',1' : '' extra = extra ? (',' + extra) : '' - return `${DATA_ROOT}(${id},'${name}'${extra})` + return `${DATA_ROOT}(${id},'${name}'${isFallbackContent}${extra})` } } +function isInSlot (el) { + let parent = el.parent + while (parent) { + if (parent.tag === 'slot') { + return true + } + parent = parent.parent + } + return false +} + // if 使用该方案是因为 template 节点之类无法挂靠 extras function processIfConditions (el) { if (el.if) { el.ifConditions.forEach(con => { if (isVar(con.exp)) { - con.exp = createGenVar(con.block.attrsMap[ID])(con.block.elseif ? 'v-else-if' : 'v-if') + con.exp = createGenVar(con.block.attrsMap[ID], isInSlot(el))(con.block.elseif ? 'v-else-if' : 'v-if') } }) - el.if = createGenVar(el.attrsMap[ID])('v-if') + el.if = createGenVar(el.attrsMap[ID], isInSlot(el))('v-if') } } @@ -48,7 +60,7 @@ function processBinding (el, genVar) { function processFor (el, genVal) { if (el.for && isVar(el.for)) { - el.for = createGenVar(el.forId)('v-for') + el.for = createGenVar(el.forId, isInSlot(el))('v-for') //
  • // => //
  • @@ -75,7 +87,7 @@ function processKey (el) { el.key = `${forEl.alias}['k${keyIndex}']` } } else { - isVar(el.key) && (el.key = createGenVar(el.attrsMap[ID])('a-key')) + isVar(el.key) && (el.key = createGenVar(el.attrsMap[ID], isInSlot(el))('a-key')) } } } @@ -109,8 +121,8 @@ function processText (el, parent) { const state = { index: 0, view: true, - genVar: createGenVar(parent.attrsMap[ID]) - } + genVar: createGenVar(parent.attrsMap[ID], isInSlot(parent)) + } // fixed by xxxxxx 注意:保持平台一致性,trim 一下 el.expression = parseText(el.text.trim(), false, state).expression } @@ -127,7 +139,7 @@ function transformNode (el, parent, state) { } const id = el.attrsMap[ID] - const genVar = createGenVar(id) + const genVar = createGenVar(id, isInSlot(el)) processFor(el, genVar) processKey(el) diff --git "a/src/platforms/app-plus/view/framework/plugins/\346\270\262\346\237\223.md" "b/src/platforms/app-plus/view/framework/plugins/\346\270\262\346\237\223.md" deleted file mode 100644 index c435c7c0f..000000000 --- "a/src/platforms/app-plus/view/framework/plugins/\346\270\262\346\237\223.md" +++ /dev/null @@ -1,25 +0,0 @@ -1.service new VuePageComponent -2.service beforeCreate nodeId -3.service page mounted send first data - -first render -//第一次渲染, -//1.需要带有页面 id,页面路径等 -//2.首次渲染的 nodeId 列表 -//3.数据 -//二次渲染 -//1.新增的 nodeId -//2.数据 -[ - [init,[pageId,{}]], - [created,['1','2']], - [mounted,[['1',{}],['2',{}]]] -] - -//首次处理流程 -//1.接收到命令后,创建 vd -//2.创建页面 vm,beforeCreate 中赋值页面数据,赋值 nodeId -//二次处理流程 -//1.接收到命令后,根据 nodeId 查找 vm,存在,则赋值+forceUpdate, - - 即 beforeCreate 中提取 nodeId, \ No newline at end of file -- GitLab