javascript - 使用 eval() 获取对导入的引用

标签 javascript vue.js

我正在使用一个表单小部件,其中表单是由从我的 api 检索到的架构生成的。我正在使用的表单库具有内置验证器( vue-form-generator ),可以通过库直接引用。例如:

"schema": {
  "fields": [{
    "type": "input",
    "inputType": "email",
    "label": "E-mail",
    "model": "email",
    "placeholder": "Email",
    "validator": "VueFormGenerator.validators.email",
    "required": true
  },{
    "type": "submit"
  }]
}

这是我的代码:

<template>
  <div id="app" v-if="loaded">
    <vue-form-generator :schema="schema" :model="model" :options="formOptions"></vue-form-generator>
  </div>
</template>

<script>
  /* eslint no-eval: 0 */

  import Vue from 'vue'
  import VueFormGenerator from 'vue-form-generator/dist/vfg-core.js'
  import 'vue-form-generator/dist/vfg-core.css'

  Vue.use(VueFormGenerator)

  export default {
    name: 'app',

    data: function () {
      return {
        loaded: false,
        model: null,
        schema: null,
        formOptions: null
      }
    },

    created: function () {
      return fetch('http://localhost:3000/forms')
        .then((response) => response.json())
        .then((json) => {
          // evaluate the validator
          json.schema.fields.map((field) => {
            if (field.validator) {
              field.validator = eval(field.validator)
            }
            return field
          })

          // assign the schema and model
          this.model = json.model
          this.schema = json.schema
          this.formOptions = json.formOptions

          // indicate that the schema has bee loaded
          this.loaded = true
        })
        .catch((ex) => {
          console.log('parsing failed', ex)
        })
    }
  }
</script>

我的想法是,我可以使用 eval() 来评估验证器的名称并将其转换为真正的函数。但是,我收到以下错误:

parsing failed ReferenceError: VueFormGenerator is not defined
    at eval (eval at <anonymous> (eval at <anonymous> (http://localhost:8080/app.js:797:1)), <anonymous>:1:1)
    at eval (eval at <anonymous> (http://localhost:8080/app.js:797:1), <anonymous>:35:29)
    at Array.map (native)
    at eval (eval at <anonymous> (http://localhost:8080/app.js:797:1), <anonymous>:33:26)
    at <anonymous>

有没有办法做到这一点,或者在本地创建翻译以将验证器名称映射到实际函数是更好的选择吗?

谢谢!

最佳答案

您可以尝试使用reduce:

      json.schema.fields.map((field) => {
        if (field.validator) {
          // get every key after VueFormGenerator
          const keys = field.validator.split('.').slice(1)
          // use reduce to get back the deepest prop
          field.validator = keys.reduce((obj, key) => obj[key], VueFormGenerator)
        }
        return field
      })

关于javascript - 使用 eval() 获取对导入的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44272107/

相关文章:

javascript - 视觉 : How to bind property dynamically to a new DOM element

javascript - js 生成一个在 20 秒内上升的随机数

javascript - 通过 Google Sheets Apps 脚本访问 Telegram Bot API 文件

javascript - 调整 Canvas 大小而不裁剪

vue.js - "You are binding v-model directly to a v-for iteration alias"

vue.js - 使用Vue CLI3.0创建多页应用程序,如何处理此错误?

javascript - 如何将父级的 v-model 传递到模板中

javascript - 使用 javascript 更改 IE 10 中的背景图像

javascript - p5.j​​s 无法读取数组的随机值

css - 如何设置组件最大高度?