| 12345678910111213141516171819202122232425 |
- // utils/evaluate.ts
- export function createConditionEvaluator(component: any) {
- return (condition: string): boolean => {
- if (!condition) return true
-
- try {
- // 使用 with 语句创建一个作用域,包含组件实例的所有属性
- const fn = new Function('component', `
- with (component) {
- try {
- return ${condition}
- } catch (e) {
- console.error('条件执行错误:', e)
- return false
- }
- }
- `)
-
- return fn(component)
- } catch (error) {
- console.error('条件评估失败:', error, condition)
- return false
- }
- }
- }
|