-
Notifications
You must be signed in to change notification settings - Fork 667
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: improve slots option #813
Changes from 3 commits
9bcb99d
bb24a36
1a9dc0a
970eac9
11922f5
388e2d5
a0d124c
7a896ab
b9f4257
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
// @flow | ||
|
||
import Vue from 'vue' | ||
import { compileToFunctions } from 'vue-template-compiler' | ||
|
||
function startsWithTag (str: SlotValue): boolean { | ||
|
@@ -9,7 +10,8 @@ function startsWithTag (str: SlotValue): boolean { | |
function createVNodesForSlot ( | ||
h: Function, | ||
slotValue: SlotValue, | ||
name: string | ||
name: string, | ||
_Vue: any | ||
): VNode | string { | ||
if (typeof slotValue === 'string' && !startsWithTag(slotValue)) { | ||
return slotValue | ||
|
@@ -18,22 +20,41 @@ function createVNodesForSlot ( | |
const el = | ||
typeof slotValue === 'string' ? compileToFunctions(slotValue) : slotValue | ||
|
||
const vnode = h(el) | ||
let vnode = h(el) | ||
if (typeof slotValue === 'string') { | ||
const vue = new Vue() | ||
const _vue = new _Vue() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not just use the _vue.renderProxy rather than mixing both? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since it raises an error. |
||
for (const key in _vue._renderProxy) { | ||
if (!(vue._renderProxy[key])) { | ||
vue._renderProxy[key] = _vue._renderProxy[key] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not always use _vue.renderProxy? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am sorry. |
||
} | ||
} | ||
try { | ||
// $FlowIgnore | ||
vnode = el.render.call(vue._renderProxy, h) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do components without a render function work here? For example: mount(TestComponent,{
slots: { default: {
template: '<div />'
}}
}) Can you add a test for this kind of slot |
||
vnode = h(vnode.tag, vnode.data || {}, vnode.children) | ||
} catch (e) { | ||
} | ||
} | ||
|
||
vnode.data.slot = name | ||
return vnode | ||
} | ||
|
||
export function createSlotVNodes ( | ||
h: Function, | ||
slots: SlotsObject | ||
slots: SlotsObject, | ||
_Vue: any | ||
): Array<VNode | string> { | ||
return Object.keys(slots).reduce((acc, key) => { | ||
const content = slots[key] | ||
if (Array.isArray(content)) { | ||
const nodes = content.map(slotDef => createVNodesForSlot(h, slotDef, key)) | ||
const nodes = content.map( | ||
slotDef => createVNodesForSlot(h, slotDef, key, _Vue) | ||
) | ||
return acc.concat(nodes) | ||
} | ||
|
||
return acc.concat(createVNodesForSlot(h, content, key)) | ||
return acc.concat(createVNodesForSlot(h, content, key, _Vue)) | ||
}, []) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<template> | ||
<div><span baz="qux">{{ fromLocalVue }},{{ bar }}</span></div> | ||
</template> | ||
|
||
<script> | ||
export default{ | ||
name: 'component-with-parent-name', | ||
props: ['fromLocalVue'], | ||
data () { | ||
return { | ||
bar: 'quux' | ||
} | ||
}, | ||
mounted () { | ||
this.$parent.childComponentName = this.$options.name | ||
} | ||
} | ||
</script> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would be a problem if you mount a component that uses localVue and the slot component accesses a property that is only added to the localVue prototype, can you write a test to check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is.
Changing
localVue.prototype
, this does not work.Using
vm._renderProxy
is correct if possible.I will add test for changing
localVue.prototype
.