4

I am using the default Laravel 5.3 setup - a fresh install with default configuration for Vue.js 2.0.

With Laravel 5.1 and Vue.js 1.x, I could easily define components like and used browserify to compile.

Vue.component('test-component', require('./test-component');  

/*  test-component.js */
export default{
    template:require('./test-component.template.html')
}

/* test-component.template.html  */
<div class="test-component">
    <h1> Test Component <h1>
</div>  

However, with Vue 2, the default is webpack (although browserify is also available but could not get it working and webpack seems better). But I am not able to get the configuration working.

I have tried various configurations, finally I have this in my gulp.js file

const elixir = require('laravel-elixir');
require('laravel-elixir-vue-2'); 

var config = {

    module:{
        loaders:[
            {
                test: /\.html$/,
                loader: 'vue-loader'
            }
        ]
    }
};
elixir(mix => {
    mix.sass('app.scss')
       .webpack('app.js',null, null, config);
});  

Now I am not getting any errors while compiling gulp webpack however when I try to view the page in the browser, it doesn't show the component and has an error/warn in the console

vue.js?3de6:513[Vue warn]: invalid template option:[object Object] 
(found in component <test>)

vue.js?3de6:4085Uncaught TypeError: Cannot read property 'child' of null(…)  

My app.js main entry file (default provided by Laravel)

require('./bootstrap');

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

const app = new Vue({
    //el: '#app',
}).$mount('#app');  

What am I missing? Any pointers would be appreciated.

2 Answers 2

3
  1. You have to use html-loader instead of vue-loader.

    npm install html-loader --save-dev
    
  2. Your gulpfile.js (need to change the loader):

    const config = {
      module: {
        loaders:[{
          test: /\.html$/,
          loader: 'html'
        }]
      }
    };
    
    elixir((mix) => {
      mix.sass('app.scss')
        .webpack('app.js', null, null, config);
    });
    
  3. Your test-component.js (no changes, you're good to go):

    export default {
      template: require('./test-component.template.html')
    }
    
  4. Your test-component-template.html: (no changes, you're good to go)

    <div class="test-component">
      <h1>Test Component Goes Here</h1>
    </div>
    

Important

  1. Here's how to define your component:

    • Using Default

      Vue.component('test-component', require('./test-component').default);
      
    • Or, simply use ES2015 import

      import TestComponent from './test-component';
      
      Vue.component('test-component', TestComponent);
      
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much. I had been struggling with this since 2 days. I had tried with vue-html-loader but didn't work. Now its working with the import statement but doesn't work with Vue.component('test-component', require('./test-component').default);. Still checking my code for any typos.
Glad to help you, Sir @Donkarnash :)
My bad, I had a typo. It works with both import as well as Vue.component('test-component', require('./test-component').default);. Thank you again.
Oh, I didn't notice that! Great! One consideration, I think using html-loader is a bit faster than using vue-loader. Because vue-loader will try to parse template, script, and style tag; while html-loader will only return the html content on your *.template.html file. But, still, it's all up to you, Sir :)
0

Try this in your gulp file:

 const elixir = require('laravel-elixir');
 require('laravel-elixir-vue-2');

 elixir(mix => {
     mix.webpack('app.js');
 });

And import your components like this:

import Test from './Components/Test.vue';

Vue.component('test', Test);

1 Comment

This works with .vue files. I don't have issues with .vue files for components. I have issues when I have template for the component in a htmlfile while the script for the component is in a .js file from where I do a template:require('./test-component.template.html')

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.