Summer Sale! All accounts are 50% off this week.

fdusautoir's avatar

Vue and alias

Hi Everybody.

How can I have aliases in my vue files ? to prevent to use relative path everywhere ... That is not very convenient if I change the app structure in a future.

for example in my ./store/index.js I have in a function :

router.addRoutes([
    {
        path: '/clients',
        component: require('../../views/Clients.vue')
    }
]);
                

The best would be (as in Laravel) :

router.addRoutes([
    {
        path: '/clients',
        component: require(views_path('/Clients.vue'))
    }
]);

What is the best way to achieve that ?

Thanks for your help ! :)

0 likes
7 replies
Cronix's avatar

I think it would be better to add it to js in the master template blade view, and then access that.

<script>
    window.view_path = '{{ base_path("resources/views") }}';
</script>

and then in javascript

component: require(view_path+'/Clients.vue')
fdusautoir's avatar

Mmm I don't think that is a clean way to achieve that. I'm thinking about webpack.mix and aliases but I don't understand how it works. @JeffreyWay any videos or help about this ?

fdusautoir's avatar
fdusautoir
OP
Best Answer
Level 11

That's okay, in my webpack.mix.js file I've added this and that's works fine :

let src_path = 'resources/assets/js/';

mix.webpackConfig({
  resolve: {
    alias: {
      '@src': path.resolve(__dirname, src_path),
      '@store': path.resolve(__dirname, src_path + 'store/'),
      '@views': path.resolve(__dirname, src_path + 'views/'),
      '@components': path.resolve(__dirname, src_path + 'components/'),
      '@modules': path.resolve(__dirname, src_path + '/modules/'),
    }
  }
});

``

Please or to participate in this conversation.