0

I'm building a relatively simple laravel application and wanted to learn Vue.js... which is turning into a super frustrating mess...

Anyway, here's the issue. No matter what I do, I get the error that my component isn't defined.

Here's what I have...

Gulpfile

var elixir = require('laravel-elixir');
  require('laravel-elixir-vueify');

// Elixir Mixins.
elixir(function(mix) {
    mix.sass('app.scss');
    mix.browserify('main.js');
});

/resources/assets/js/main.js

   var Vue =  require('vue');
   var VueRouter = require('vue-router');
   Vue.use(VueRouter);
   import Form2016_1099_misc from './components/2016-1099-misc.vue';
   Vue.component('Form2016_1099_misc', Form2016_1099_misc);

In my blade file...

@section('content')
  <div id="app">
    <h1>Hello App!</h1>
    <p>
      <!-- use v-link directive for navigation. -->
      <a v-link="{ path: '/recipient' }">Go to Recipient</a>
      <a v-link="{ path: '/form/2016/1099-misc' }">Go to 1099</a>
    </p>
    <!-- route outlet -->
    <router-view></router-view>
  </div>
@endsection

@section('footer')
  <script src="{{ asset('/js/main.js') }}"></script>

  <script>
    // Define some components
    var RecipientForm = Vue.extend({
      template: '<p>This is the recipient form.</p>'
    });
    var App = Vue.extend({});
    var router = new VueRouter();

    router.map({
      '/recipient': {
        component: RecipientForm
      },

      '/form/2016/1099-misc': {
        component: Form2016_1099_misc
      },
    });
    router.start(App, '#app');
  </script>
@endsection

/resources/assets/js/components/2016-1099-misc.vue

<template>
    {{ msg }}
</template>

<style>

</style>

<script>
    export default{
        data(){
            return{
                msg: 'This is a test'
            }
        }
    }
</script>

I run gulp and it operates successfully. When I go to view the page in my browser, I get the error message

ReferenceError: Form2016_1099_misc is not defined

I'm certain that I'm doing a bunch of things wrong. I'm super new to Vue and I'm struggling with grasping it...

Update:

I have moved all the script code from my blade file into the main.js file and now main.js looks like this -

var Vue =  require('vue');
var VueRouter = require('vue-router');
use(VueRouter);
import Form2016_1099_misc from './components/2016-1099-misc.vue';
Vue.component('Form2016_1099_misc', Form2016_1099_misc);

// Define some components
var RecipientForm = Vue.extend({
  template: '<p>This is the recipient form.</p>'
});

var App = Vue.extend({});
var router = new VueRouter();

router.map({
  '/recipient': {
    component: RecipientForm
  },

  '/form/2016/1099-misc': {
    component: Form2016_1099_misc
  },


});

router.start(App, '#app');

I now get the error message

[Vue warn]: Failed to resolve directive: link (found in component: <router-app>)
[Vue warn]: Unknown custom element: <router-view> - did you register the component correctly? For recursive components, make sure to provide the "name" option.
0

2 Answers 2

0

You need to register the component with Vue. You are telling the router to build a component view when the route changes, but Vue also needs to know about the component.

After you import your component, try: Vue.component('Form2016_1099_misc', Form2016_1099_misc);

See: https://vuejs.org/guide/components.html#Registration

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

1 Comment

I added the Vue.component('Form2016_1099_misc', Form2016_1099_misc); to my main.js file and ran Gulp again (updated the main post here too) and there's no change. When I load the page in the browser, I still get the same "Form2016_1099_misc is not defined"
0

I was apparently loading Vue twice. Once from NPM and the other from the CDN. I removed the CDN and now it works fine...

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.