Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJS Module not found error while importing the components

I have an app where I load component using <router-view/>!

I am getting the problem in importing the component in router.js. It always gives me the following error:

These relative modules were not found:

* ./components/Poetry.vue in ./src/router/routes.js
* ./components/PoetryDetails/PoetryCard.vue in ./src/router/routes.js
* ./components/PoetryDetails/Search.vue in ./src/router/routes.js

I have the folder structure like this:

├── _components
|   ├── _PoetryDetails
|   |   └──PoetryCard.vue
|   |   └──Search.vue
|   └── Poetry.vue
├── _router
|   ├── index.js (routes.js is imported in this file)
|   └── routes.js
├── App.vue
└── main.js

In routes.js, I am importing the components as follows:

import Poetry from './components/Poetry.vue'
import PoetryCard from './components/PoetryDetails/PoetryCard.vue'
import PoetrySearch from './components/PoetryDetails/Search.vue'

export const routes = [
    { path: '', name: 'poetrysearch', component: Poetry, children: [
        { path: 'poetry', name: 'poetrycard', component: PoetryCard },
        { path: 'poetry/search', name: 'poetrysearch', component: PoetrySearch },
    ]},
]

But, I am still getting the damn error. Please help.

like image 394
Damon Avatar asked Jul 12 '18 02:07

Damon


1 Answers

You're using relative path. In your routes.js, it should be

import Poetry from '../components/Poetry.vue'
import PoetryCard from '../components/PoetryDetails/PoetryCard.vue'
import PoetrySearch from '../components/PoetryDetails/Search.vue'

.. notation means go to parent folder.

If you're using webpack, you can add alias:

  resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      ...
      '@': resolve('src'), // your source folder
    }
  }

and then you can import:

import Poetry from '@/components/Poetry.vue'
import PoetryCard from '@/components/PoetryDetails/PoetryCard.vue'
import PoetrySearch from '@/components/PoetryDetails/Search.vue'
like image 139
ittus Avatar answered Nov 05 '22 00:11

ittus