Answer the question
In order to leave comments, you need to log in
How to display data with Vue js?
Good to everyone!
I'm trying to display data from a database using Vue js.
<div class="container">
<h1>My Tasks</h1>
<tasks list="{{ json_encode($tasks) }}" v-on:parse="getParsed"></tasks>
</div>
<template id="tasks-template">
<div>
<ul class="list-group">
<li class="list-group-item" v-for="task in list">@{{ task.id }}</li>
</ul>
</div>
</template>
$tasks
- a variable containing data from the database.Vue.component('tasks', {
template: '#tasks-template',
props: ['list'],
created: function () {
this.$emit('parse', this.list);
}
});
new Vue({
el: '.container',
data: {
list: {}
},
methods: {
getParsed: function ($parsedList) {
this.list = JSON.parse($parsedList);
}
}
});
<pre>@{{ list }}</pre>
console.log(this.list);
prints the object: Answer the question
In order to leave comments, you need to log in
To remove emit
from the child component, v-on
from the parent - this is just garbage code that does not do any useful work. Parameter list
- replace static value transfer with dynamic one: :list="{{ json_encode($tasks) }}"
. So the passed value will be considered not as a string, but as a javascript expression - JSON.parse
no need to apply.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question