evaluate.ts 802 B

12345678910111213141516171819202122232425
  1. // utils/evaluate.ts
  2. export function createConditionEvaluator(component: any) {
  3. return (condition: string): boolean => {
  4. if (!condition) return true
  5. try {
  6. // 使用 with 语句创建一个作用域,包含组件实例的所有属性
  7. const fn = new Function('component', `
  8. with (component) {
  9. try {
  10. return ${condition}
  11. } catch (e) {
  12. console.error('条件执行错误:', e)
  13. return false
  14. }
  15. }
  16. `)
  17. return fn(component)
  18. } catch (error) {
  19. console.error('条件评估失败:', error, condition)
  20. return false
  21. }
  22. }
  23. }