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

fix(mp): SETUP_REACTIVE_CONST (#3606)

上级 788a61e2
......@@ -220,6 +220,17 @@ export function render(_ctx, _cache) {
})
})
test('setup-reactive-const', () => {
const { code } = parseWithElementTransform(`{{state.test}}`, {
inline: true,
prefixIdentifiers: true,
bindingMetadata: {
state: BindingTypes.SETUP_REACTIVE_CONST,
},
})
expect(code).toContain(`_t(state.test)`)
})
describe('dynamic component', () => {
test('static binding', () => {
const onError = jest.fn()
......
......@@ -149,7 +149,9 @@ function resolveSetupReference(name: string, context: TransformContext) {
}
}
const fromConst = checkType(BindingTypes.SETUP_CONST)
const fromConst =
checkType(BindingTypes.SETUP_CONST) ||
checkType(BindingTypes.SETUP_REACTIVE_CONST)
if (fromConst) {
return context.inline
? // in inline mode, const setup bindings (e.g. imports) can be used as-is
......
import { isString, hasOwn, makeMap, isGloballyWhitelisted } from '@vue/shared'
import {
isString,
hasOwn,
makeMap,
isGloballyWhitelisted,
genPropsAccessExp,
} from '@vue/shared'
import {
Node,
Identifier,
......@@ -74,6 +80,9 @@ interface PrefixMeta {
scopeIds?: Set<string>
}
// Important: since this function uses Node.js only dependencies, it should
// always be used with a leading !__BROWSER__ check so that it can be
// tree-shaken from the browser build.
export function processExpression(
node: SimpleExpressionNode,
context: TransformContext,
......@@ -84,7 +93,7 @@ export function processExpression(
asRawStatements = false,
localVars: Record<string, number> = Object.create(context.identifiers)
): ExpressionNode {
if (!node.content.trim()) {
if (!context.prefixIdentifiers || !node.content.trim()) {
return node
}
......@@ -102,7 +111,11 @@ export function processExpression(
const isDestructureAssignment =
parent && isInDestructureAssignment(parent, parentStack)
if (type === BindingTypes.SETUP_CONST || localVars[raw]) {
if (
type === BindingTypes.SETUP_CONST ||
type === BindingTypes.SETUP_REACTIVE_CONST ||
localVars[raw]
) {
return raw
} else if (type === BindingTypes.SETUP_REF) {
return `${raw}.value`
......@@ -161,12 +174,17 @@ export function processExpression(
} else if (type === BindingTypes.PROPS) {
// use __props which is generated by compileScript so in ts mode
// it gets correct type
return `__props.${raw}`
return genPropsAccessExp(raw)
} else if (type === BindingTypes.PROPS_ALIASED) {
// prop with a different local alias (from defineProps() destructure)
return genPropsAccessExp(bindingMetadata.__propsAliases![raw])
}
} else {
if (type && type.startsWith('setup')) {
// setup bindings in non-inline mode
return `$setup.${raw}`
} else if (type === BindingTypes.PROPS_ALIASED) {
return `$props['${bindingMetadata.__propsAliases![raw]}']`
} else if (type) {
return `$${type}.${raw}`
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册