xaboy / form-create-designer

好用的Vue低代码可视化表单设计器,可以通过拖拽的方式快速创建表单,提高开发者对表单的开发效率。
https://view.form-create.com/
MIT License
1.58k stars 361 forks source link

选择器加载异步数据token #84

Open fengbindev opened 1 year ago

fengbindev commented 1 year ago

fc-designer的选择器组件,在加载异步数据的时候怎么操作才能解决待token得问题???就是访问异步数据接口的时候能自带vue中的token??? 如: $store.user.token ,这种方式

xaboy commented 1 year ago

自己重写formCreate.fetch方法

OlyLis1005 commented 1 year ago

修改这个文件 src/plugins/formCreate/index.ts

interface Option {
  action: string
  method?: AxiosMethod
  data?: object
  dataType?: object
  headers?: object
  withCredentials?: boolean
  onSuccess: (body: any) => void
  onError?: (e: Error | ProgressEvent) => void
  _parse?: string
}

// 参考 http://www.form-create.com/v3/element-ui/auto-import.html 文档
export const setupFormCreate = (app: App<Element>) => {
  components.forEach((component) => {
    app.component(component.name, component)
  })
  formCreate.use(install)

  const overrideFetch = (option: Option, effectArgs) => {
    // console.log('--fetch option--', option)

    serivice({
      url: option.action,
      method: option.method,
      data: option.data,
      headers: option.headers,
      withCredentials: option.withCredentials
    })
      .then((res) => {
        try {
          if (option._parse) {
            const parseFn = formCreate.parseFn(option._parse)
            // console.log('--parseFn--', parseFn)
            res.data = parseFn(res)
          }
        } catch (e) {
          console.log('--parseFn error--', e)
        }
        option.onSuccess?.(res)
      })
      .catch((e) => {
        option.onError?.(e)
      })

    return option
  }
  // 重写 fetch 请求方法,select 等动态获取数据请求通过拦截器
  formCreate.fetch = overrideFetch

  app.use(formCreate)
}
rujianwang commented 6 months ago

自己重写formCreate.fetch方法

请问下这个要怎么重写

xaboy commented 6 months ago

@rujianwang 看一下楼上的代码, 核心就是formCreate.fetch = overrideFetch 覆盖formCreate.fetch

rujianwang commented 6 months ago

@rujianwang 看一下楼上的代码, 核心就是formCreate.fetch = overrideFetch 覆盖formCreate.fetch

Vue.use(ELEMENT);

formCreate.fetch = function(option, effectArgs) { debugger; }; Vue.use(formCreate);

我在main.js里面这样写 默认不生效。我不太想 改你的源码