0

I'm Dynamically loading vue components. However it can't find the path. How can I set webpack so I can add the full path into the array and not concat it in the require?

Does not work

Vue.component(title, require(file));
<sau-page-nav :components="{{json_encode(['AddCredit'=>'../billing/AddCredit','PayPal'=>'../billing/PayPal'])}}"></sau-page-nav>

Works

Vue.component(title, require('../' + file));

<sau-page-nav :components="{{json_encode(['AddCredit'=>'billing/AddCredit','PayPal'=>'billing/PayPal'])}}"></sau-page-nav>
2
  • 1
    Are you asking why the second works, is that it? Commented Apr 4, 2018 at 4:18
  • Yes...Updated question Commented Apr 4, 2018 at 7:04

1 Answer 1

1

You can get your webpack.config to resolve a shortcut to a path...

This is from the webpack that comes when you initiate a project with with Vue cli...

  resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      'vue$': 'vue/dist/vue.esm.js',
      '@': resolve('src'),
      modernizr$: path.resolve(__dirname, "../.modernizrrc")
    }
  },

It's the @ bit that's important - see how if references the src directory. This allows you to reference components without the full path. Like this...

Vue.component('test', require('@/components/my-component'))

or using an import...

import myComponent from '@/components/my-component'

Hope that helps

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

Comments

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.