nocobase/packages/core/client/src/variables
被雨水过滤的空气-Rain 454db91827
fix: fix regular of variable (#3024)
* fix: fix regular of variable

* refactor: the better way
2023-11-11 11:23:34 +08:00
..
__tests__ fix: fix regular of variable (#3024) 2023-11-11 11:23:34 +08:00
hooks fix: avoid infinite loop (#2974) 2023-11-06 14:21:28 +08:00
utils fix: fix regular of variable (#3024) 2023-11-11 11:23:34 +08:00
constants.ts
index.ts
README.md
README.zh-CN.md
types.ts
VariablesProvider.tsx fix: fix regular of variable (#3024) 2023-11-11 11:23:34 +08:00

useVariables

Usage

const { registerVariable, parseVariable } = useVariables();

// Register variable
registerVariable({
  name: '$user',
  // If you are certain that the `association field` data does not need to be loaded on-demand in the variable, you can omit this field
  collectionName: 'users',
  ctx: {
    name: 'Zhang San',
    nickname: 'Xiao Zhang',
  },
});

// Parse variable
const userName = await parseVariable('{{ $user.name }}');
console.log(userName); // 'Zhang San'

Built-in Global Variables

Some variables that are used globally are registered within the VariablesProvider. These variables are defined in useBuiltinVariables and can be modified by changing the return value of useBuiltinVariables.

Local Variables

When using the parseVariable method in useVariables, in addition to parsing based on the built-in global variables, you can also use some temporary local variables.

const { parseVariable } = useVariables();
const localVariable = {
  name: '$user',
  // If you are certain that the `association field` data does not need to be loaded on-demand in the variable, you can omit this field
  collectionName: 'users',
  ctx: {
    name: 'Zhang San',
    nickname: 'Xiao Zhang',
  },
}

// Use local variable for parsing
const userName = await parseVariable('{{ $user.name }}', localVariable);
console.log(userName); // 'Zhang San'

The registered local variables will be automatically destroyed after parsing and will not affect the value of global variables.

useLocalVariables

This hook encapsulates some variables that are quite common but cannot be treated as global variables.