Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webpack配置路径别名 #57

Open
artdong opened this issue May 6, 2020 · 1 comment
Open

webpack配置路径别名 #57

artdong opened this issue May 6, 2020 · 1 comment
Labels
webpack 打包优化

Comments

@artdong
Copy link
Collaborator

artdong commented May 6, 2020

webpack配置路径别名

@artdong artdong added the webpack 打包优化 label May 6, 2020
@artdong
Copy link
Collaborator Author

artdong commented May 6, 2020

为什么需要配置路径别名

模块化开发项目中,比如vue和react等,经常需要import不同的js或者css模块到当前目录,那么如果路径比较深的话使用相对路径就比较麻烦,而且在文件迁移的时候,如果在不同的目录下边,又得改变以下引入的路径。所以我们可以使用webpack配置路径别名,在引入的时候我们直接使用这个别名,就可以正确的指向。

如何配置

在webpack.config.dev中的resolve下的alias属性做以下配置:

alias: {
      "@src":path.resolve("src"),
      "@components":path.resolve("src/components"),
      "@pages":path.resolve("src/pages"),
      "@utils":path.resolve("src/utils"),
    },

使用方法

import Home from "@pages/home/home"
import userForm from '@components/form/user/user_form.jsx';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
webpack 打包优化
Projects
None yet
Development

No branches or pull requests

1 participant