Vue模板表达式中所能访问的全局变量

Vue模板表达式中所能访问的全局变量

Vue的模板表达式是放在沙盒中的,只能访问一个受限的全局变量列表。

具体在模板表达式中可以访问到哪些全局变量,如下。

code: https://github.com/vuejs/core/blob/main/packages/shared/src/globalsWhitelist.ts#L3

1
2
3
4
5
6
7
8
import { makeMap } from './makeMap'

const GLOBALS_WHITE_LISTED =
'Infinity,undefined,NaN,isFinite,isNaN,parseFloat,parseInt,decodeURI,' +
'decodeURIComponent,encodeURI,encodeURIComponent,Math,Number,Date,Array,' +
'Object,Boolean,String,RegExp,Map,Set,JSON,Intl,BigInt'

export const isGloballyWhitelisted = /*#__PURE__*/ makeMap(GLOBALS_WHITE_LISTED)

同时,使用模板时应该遵守一条原则:模板中的表达式不应该有副作用,比如更改数据或者触发异步进程。

评论