6

I've been building my Vue application by using Vue Cli 3.

If I need to import an index.js which is in directory named Dir1, I can import it using import file1 from '@/components/Dir1/ but somehow it doesn't work with .vue extension files.

I have to expicitly mention the file name such as import Title from @/components/Title/index.vue.

What changes do I have to make in the settings in order to import the .vue extension file without mentioning the filename?

3
  • This works just fine for me. Can you provide some more context, or a repo with your problem? Commented Jan 26, 2019 at 18:28
  • 1
    Post your webpack configuration Commented Jan 27, 2019 at 2:11
  • Did you get your problem resolved? I have the same issue and can't seem to find the solution. My project is initialized with Vue's CLI 3 Commented Nov 19, 2019 at 15:26

1 Answer 1

2

This is how I would do it with Vue.

You may need to tweak the config a little bit to suit your dev environment needs. Note that this is not a full config but a guideline on what should be done based on NPM directory-named-webpack-plugin documentation.

In your webpack.config.js you should have the following (Webpack 3):

const DirectoryNamedWebpackPlugin = require('directory-named-webpack-plugin');
// ...
let config = {
  // ...
  module: {
    rules: [
      {
        test: /\.(js|vue)$/,
        exclude: /node_modules/,
        use: 'babel-loader'
      }
    ]
  },
  resolve: {
    modules: ['components', 'node_modules'],
    extensions: ['.js', '.vue'],
    plugins: [
      new DirectoryNamedWebpackPlugin(true)
    ]
  }
  // ...
}
modules.exports = config;

taken and modified for Vue from: Recursive import of components using webpack in React

Sign up to request clarification or add additional context in comments.

1 Comment

how about Vite?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.