refactor(template-ref): improve template ref handling
close #836, close #839
This commit is contained in:
@@ -4,7 +4,6 @@ import { transform, NodeTransform, DirectiveTransform } from './transform'
|
||||
import { generate, CodegenResult } from './codegen'
|
||||
import { RootNode } from './ast'
|
||||
import { isString } from '@vue/shared'
|
||||
import { transformRef } from './transforms/transformRef'
|
||||
import { transformIf } from './transforms/vIf'
|
||||
import { transformFor } from './transforms/vFor'
|
||||
import { transformExpression } from './transforms/transformExpression'
|
||||
@@ -28,7 +27,6 @@ export function getBaseTransformPreset(
|
||||
): TransformPreset {
|
||||
return [
|
||||
[
|
||||
transformRef,
|
||||
transformOnce,
|
||||
transformIf,
|
||||
transformFor,
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
import { NodeTransform } from '../transform'
|
||||
import {
|
||||
NodeTypes,
|
||||
ElementTypes,
|
||||
createSimpleExpression,
|
||||
createCompoundExpression
|
||||
} from '../ast'
|
||||
import { findProp } from '../utils'
|
||||
|
||||
// Convert ref="foo" to `:ref="[_ctx, 'foo']"` so that the ref contains the
|
||||
// correct owner instance even inside slots.
|
||||
export const transformRef: NodeTransform = node => {
|
||||
if (
|
||||
!(
|
||||
node.type === NodeTypes.ELEMENT &&
|
||||
(node.tagType === ElementTypes.ELEMENT ||
|
||||
node.tagType === ElementTypes.COMPONENT)
|
||||
)
|
||||
) {
|
||||
return
|
||||
}
|
||||
const ref = findProp(node, 'ref')
|
||||
if (!ref) return
|
||||
const refKey =
|
||||
ref.type === NodeTypes.ATTRIBUTE
|
||||
? ref.value
|
||||
? createSimpleExpression(ref.value.content, true, ref.value.loc)
|
||||
: null
|
||||
: ref.exp
|
||||
if (refKey) {
|
||||
node.props[node.props.indexOf(ref)] = {
|
||||
type: NodeTypes.DIRECTIVE,
|
||||
name: `bind`,
|
||||
arg: createSimpleExpression(`ref`, true, ref.loc),
|
||||
exp: createCompoundExpression([`[_ctx, `, refKey, `]`]),
|
||||
modifiers: [],
|
||||
loc: ref.loc
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user