-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(slot): add a function to return the slot fallback content #12014
Conversation
slot fallback content should be evaluated only when the parent is not providing it.
Sorry for the delay and thanks a lot @zrh122 ! |
Its render a empty node with this case; My rollup project environment
My rollup project code
When i use rollup bundle it and use bundle in demo project(init with My demo project code
I try to upgrade demo project When i reduce rollup project Did I use it right? |
Templates compiled with 2.6.14 are only compatible with projects running >=2.6.14 while templates compiled with 2.6.12 are compatible with projects running >=2.6.12 |
got it 👍 |
The code compiled by the 2.6.13+ compiler will show the slot fallback content as "undefined" when running in The problem is very easy to encounter, especially when develop a vue lib by vue-cli@latest, since CLI tool will install the latest version of In order to fix this problem in the old versions, we can only set the dependency to v2.6.12 and below hardly. eg: // package.json
{
"decDependencies": {
"vue": "2.6.12",
"vue-template-compiler": "2.6.12"
}
} And be very careful to set the version in package-lock.json, because some projects do not have a lock file at all (such as ant-desing-vue), although this is their fault. In fact, it only needs a little code to be compatible with lower versions, eg: // src/core/instance/render-helpers/render-slot.js
export function renderSlot (
name: string,
fallback: ?Array<VNode>,
props: ?Object,
bindObject: ?Object
): ?Array<VNode> {
// ...
}
renderSlot.delay = true;
// or set `_delay` prop to all instance
// src/core/instance/render-helpers/index.js
// export function installRenderHelpers (target: any) {
// ...
// target._delay = true;
//}
// and
// src/compiler/codegen/index.js:546
function genSlot (el: ASTElement, state: CodegenState): string {
const slotName = el.slotName || '"default"'
// if vue runtime supports delay evaluate slot fallback content feature,
// then return a function as current compiler do,
// or return a object array as old versions do.
const children = `
(function () {
function fallback() {
return ${genChildren(el, state)};
}
return _t.delay ? fallback : fallback();
})()
`;
let res = `_t(${slotName}, ${children}`
const attrs = el.attrs || el.dynamicAttrs
? genProps((el.attrs || []).concat(el.dynamicAttrs || []).map(attr => ({
// slot props are camelized
name: camelize(attr.name),
value: attr.value,
dynamic: attr.dynamic
})))
: null
const bind = el.attrsMap['v-bind']
if (attrs) {
res += `,${attrs}`
}
if (bind) {
res += `${attrs ? '' : ',null'},${bind}`
}
return res + ')'
} Gist: https://gist.github.com/mozhs/485222d9f51f97136134c461338786e0 |
for me, when i want to build a library, eg "element-ui/page-header"
the build result diff between use these two version:
when render the page, it's going wrong! now, I can only set the dependency to v2.6.12. |
slot fallback content should be evaluated only when the parent is not providing it.
What kind of change does this PR introduce? (check at least one)
Does this PR introduce a breaking change? (check one)
If yes, please describe the impact and migration path for existing applications:
The PR fulfills these requirements:
dev
branch for v2.x (or to a previous version branch), not themaster
branchfix #xxx[,#xxx]
, where "xxx" is the issue number)If adding a new feature, the PR's description includes:
Other information:
fixes #10224