laravel-spark

Property or method is not defined on the instance but referenced during render


I am working on laravel + spark + vuejs. I am trying to make new module and following is code.

routes/web.php

Route::get('/emergency-template-plan', 'EmergencyPlanTemplateController@emergencyTemplatePlan');

Vue js: resources/assets/js/app.js

require('spark-bootstrap');
require('./components/bootstrap');

Vue.component('spark-emergency', { 
data() {
    return {
        all_emergencies:[]
    };
},
created() {
    this.getEmergencies();
},
methods: {
    getEmergencies() {
        this.$http.get('/emergency-template-plan')
            .then(response => {
                this.all_emergencies = response.data;
            });
    }
}
});

var app = new Vue({
 mixins: [require('spark')]
});

Controller: app/Http/Controllers/EmergencyPlanTemplateController.php

public function emergencyTemplatePlan(){
  $data = EmergencyPlanTemplate::all();

   return view('spark::emergency-template-plan', ['emergencies'=>$data]);
}

View: resources/views/vendor/spark/emergency-template-plan.blade.php

@extends('spark::layouts.app')
@section('content')
<spark-emergency :all_emergencies="all_emergencies" inline-template>    
<tr v-for="emergency in all_emergencies">
    <td>
        <div class="btn-table-align">
            @{{ emergency.type }}
        </div>
    </td>
</tr>
</spark-emergency>
@endsection

I am getting following error

Vue warn]: Property or method "all_emergencies" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option (found in root instance)


Solution

  • The problem here is the all_emergencies in the parent component where you try to use the spark_emergency component.

    You try sending emergencies via props :all_emergencies="all_emergencies" which basically means that you want to send from the parent of your component some data to the child component.

    Since you initialize the emergencies inside the component itself this.$http... inside created hook, there is no need to send it as a prop. This should make it work.

    <spark-emergency inline-template>    
    <tr v-for="emergency in all_emergencies">
    <td>
        <div class="btn-table-align">
            @{{ emergency.type }}
        </div>
    </td>
    </tr>
    </spark-emergency>
    

    Further read https://v2.vuejs.org/v2/guide/components.html#Props