mardi 12 septembre 2017

Attempting to load a Vue component and populate it with Ajax json content in Laravel

I am new to Laravel and a complete noob to Vue. I searched many other Laravel/Vue posts but none seemed to be similar enough to get me to a solution. When attempting to load this component on my view I receive the following error

app.js:32654 [Vue warn]: Property or method "features" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.

Please let me know what I am missing. http://ift.tt/2xwOKnR shows that Vue is loaded I would like to load data from an ajax call to my vue component. that can be updated on the fly by event handler

App.js

window.Vue = require('vue');
Vue.component('Example', require('./components/Example.vue'));

const app = new Vue({
    el : '#app',

});

$(document).ready(function() {
    $.ajaxSetup({
        headers : {
            'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')
        }
    });
})

Example.vue

<template>
    <div class="container projects-container">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <div class="panel panel-default">
                    <div class="panel-heading">Example Component</div>

                    <div class="panel-body">
                        <h1>I'm an example component!</h1>    
                        <ul class="list-group">
                            <li class="list-group-item" v-for="feature in features">
                              
                            </li>
                        </ul>                    
                    </div>

                </div>
            </div>
        </div>
    </div>
</template>

<script>

    export default {
        mounted() {
            console.log('Component mounted.')

        },

    }

</script>

bladefile

<head>
    <meta name="csrf-token" content="INMA4kLlG32gfhf4Z3BBGIFxitrVCWzzqgqPfooEj">
// and yes Vue is loaded 
</head>
<body>
<div id="app">
    <example></example>
</div>
...

<script>
Vue.component('example',{
        template: 'Example',

        })  

//returns the JSON listed below
someOtherObject.addListener('click', function(e) {

    $.ajax({
                url:json,
                method:'GET',
                success:function(msg){
                    app.data = JSON.parse(msg);

                    }

                })
})

</script>

JSON

{
   "type":"FeatureCollection",
   "features":[
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               -117.155083,
               33.569672
            ]
         },
         "properties":{
            "heading":null,
            "face":"South",
            "status":"1",
            "name":"MEADOWLARK ",
            "type":"Single Family Home",
            "client_id":"26",
            "client_name":"Pulte Homes",
            "city_name":"French Valley"
         }
      },
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               -117.151390,
               33.543981
            ]
         },
         "properties":{
            "heading":null,
            "face":"South",
            "status":"1",
            "name":"Testing Project",
            "type":"Single Family Home",
            "client_id":"83",
            "client_name":"Testing Account",
            "city_name":"Temecula Valley"
         }
      },
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               -117.223720,
               33.571522
            ]
         },
         "properties":{
            "heading":null,
            "face":"South",
            "status":"1",
            "name":"Oak Ridge",
            "type":"Single Family Home",
            "client_id":"98",
            "client_name":"Woodside 05S LP",
            "city_name":"Beaumont"
         }
      }
   ]
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire