Stori.
Thiết lập alias imports
5 min read

Thiết lập alias imports

React Native

Sử dụng Alias imports

npx react-native@latest init PROJECT_NAME
//Thay thế PROJECT_NAME thành tên dự án của bạn 
  • File babel.config.js
  • module.exports = {
      presets: ['module:@react-native/babel-preset'],
    	// - Add this -
      plugins: [
        ...
        [
          'module-resolver',
          {
            root: ['.'],
            alias: {
              // This has to be mirrored in tsconfig.json
              '^@/(.+)': './src/\\1',
            },
          },
        ],
      ],
    	// - Add this -
    };
  • File tsconfig.json
  • {
      "extends": "@react-native/typescript-config/tsconfig.json",
    	// - Add this -
      "compilerOptions": {
        "strict": true,
        "baseUrl": ".",
        "paths": {
          //We will have to add the same thing in babel.config.js
          "@/*": ["src/*"]
        }
      }
    	// - Add this -
    }
  • Thêm file .hintrc
  • {
      "extends": ["development"],
      "hints": {
        "typescript-config/is-valid": "off"
      }
    }
    yarn add --dev babel-plugin-module-resolver
    npx pod-install ios

    Share this article

    Share:
    Read Next Article