amis icon indicating copy to clipboard operation
amis copied to clipboard

不知道有没有人遇到过initApi不起作用的情况

Open xichangzhou opened this issue 6 years ago • 9 comments

xichangzhou avatar Aug 04 '20 04:08 xichangzhou

或者使用Service组件包装也不起作用的

xichangzhou avatar Aug 04 '20 04:08 xichangzhou

你传入fetcher了么

RickCole21 avatar Aug 04 '20 04:08 RickCole21

fetcher是也不是必传项啊,而且官网上也没有说明要传啊?

{
  "type": "page",
  "initApi": "https://houtai.baidu.com/api/mock2/page/initData",
  "body": "date is ${date}"
}

不是直接这样就好了吗

xichangzhou avatar Aug 04 '20 04:08 xichangzhou

https://baidu.github.io/amis/docs/start/getting-started#%E4%BD%BF%E7%94%A8

目前需要自己实现fetcher

RickCole21 avatar Aug 04 '20 04:08 RickCole21

好的,感谢

xichangzhou avatar Aug 04 '20 05:08 xichangzhou

import React from 'react';
import { render as renderAmis } from 'amis';
import 'amis/lib/themes/default.css';
import http from '@/utils/http';
import { fetcherConfig } from 'amis/lib/factory';
import { message } from 'antd';

export default () => {
  return (
    <div style={{ width: '100%' }}>
      {renderAmis(
        {
          type: 'page',
          body: [
            {
              type: 'tpl',
              tpl: 'Hello World!',
            },
            {
              type: 'divider',
            },
            {
              type: 'form',
              initApi: 'https://houtai.baidu.com/api/mock2/form/initData',
              api: {
                url: 'https://houtai.baidu.com/api/mock2/form/saveForm',
                method: 'post',
                data: {
                  myName: '${name}',
                  myEmail: '${email}',
                },
              },
              controls: [
                {
                  type: 'text',
                  name: 'name',
                  label: '姓名:',
                },
                {
                  name: 'email',
                  type: 'email',
                  label: '邮箱:',
                },
                {
                  type: 'static-tpl',
                  tpl: '生成的id为:${id}',
                },
              ],
            },
          ],
        },
        {},
        {
          notify: (_type: string, msg: string) => {
            message.info(msg);
          },
          fetcher: (config: any) => {
            console.log(config);

            return (http as any)(config);
          },
        },
      )}
    </div>
  );
};

点击提交的时候总是提交二次,不知道到是啥原因 以下为http方法

function apiAxios({ url, data, config, method }: any) {
    console.log("-------------------------");
    console.log(url);
    console.log(data);
    console.log(config);
    console.log(method);
    console.log("-------------------------");

    config = config || {};
    config.withCredentials = true;

    if (config.cancelExecutor) {
        config.cancelToken = new (axios as any).CancelToken(config.cancelExecutor);
    }


    if (method === 'get' || method === 'delete' || method === 'patch') {
        if (data) {
            config.params = data;
        }
        return (axios as any)[method](url, config);
    } else {
        return (axios as any)[method](url, data, config);
    }
}
export default apiAxios



xichangzhou avatar Aug 04 '20 10:08 xichangzhou

跨域导致的预检请求,可以先自己查一下,应该是跟amis关系不大

RickCole21 avatar Aug 04 '20 10:08 RickCole21

小白在此感谢!

xichangzhou avatar Aug 04 '20 12:08 xichangzhou