绝对导入打字稿反应本机部分工作

Absolute import typescript react native works partially

我已按照本指南在我的项目中设置绝对导入.. 做了 Lekhnath 在这里建议的第三条评论: Absolute module path resolution in TypeScript files in VSCode 发现自己能够完美地做到这一点:

import { AppActions } from 'src/BL/redux/types/app-actions.types';

它确实起作用了。然而这不是:

import { startLoading, stopLoading } from 'src/BL/redux/action-creators/app-action-creators';

我必须提到导入很好,它甚至自动完成我和所有内容但是当我 运行 它在这个组件中失败并说它无法解析模块 action-creators.. 这是组件:

import React, { FC } from 'react';
import { AppState } from 'react-native';
import { useSelector, useDispatch } from 'react-redux';
import { useActions } from '../../../BL/redux/useActions';
import { EAAccount } from './EAAccount.interfaces';
import { isVerifiedCredentials } from './EAAccount.logic';
import NewEAAccountForm from './newEAAccountForm.view';
import { Dispatch } from 'redux';
import { AppActions } from 'src/BL/redux/types/app-actions.types';
import { startLoading, stopLoading } from 'src/BL/redux/action-creators/app-action-creators';

    const ConnectEAAccount: FC = () => {
      const dispatch = useDispatch<Dispatch<AppActions>>();
      const onSubmit = async (credentials: EAAccount) => {
        dispatch(startLoading());
        const { isVerified, err } = await isVerifiedCredentials(credentials);
        if (isVerified) {
          alert('success');
        } else alert(err);
        dispatch(stopLoading());
      };

      return <NewEAAccountForm onSubmit={onSubmit} />;
    };

当我改变

import { startLoading, stopLoading } from 'src/BL/redux/action-creators/app-action-creators';

相对导入它确实有效。 这里也有同样的问题,商店没有解决:

import store from 'src/BL/redux/configureStore';

class App extends React.Component {
  render() {
    return (
      <Provider store={store}>
        <IconRegistry icons={EvaIconsPack} />
        <ApplicationProvider {...eva} theme={eva.dark}>
          <ConnectEAAccount />
        </ApplicationProvider>
      </Provider>
    );
  }
}

在此先感谢您的帮助和时间!

解决方案是在 src 文件夹中添加 package.json 文件,其中包含:

{
    "name":"src"
}

每个要使用绝对路径的文件夹都必须添加 package.json。

还有

"paths" : {
      "src/*": ["./src/*"]
    }

不需要。