1
My code is not rendering the list produtos
defined by Vue. When I remove the template <pd>
out of the table it renders, I looked for solutions and saw that in the very document of vuejs there is a trace with the tag table
Exceptions in the Analysis of the DOM Template, but I couldn’t figure out how to use the v-for
in which case, someone could help me fix the problem?
Below is the code.
Vue.component('pd', {
props: ['nome', 'quantia'],
template: '<tr><td>{{ nome }}</td><td>{{ quantia }}</td></tr>'
});
var app = new Vue({
el: '#listaProdutos',
data: {
produtos : [
{ id: 0, nome: 'Camisa Regata', quantia: 2},
{ id: 1, nome: 'Bermuda Jeans', quantia: 5},
{ id: 2, nome: 'Casaco Preto', quantia: 1},
]
}
});
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Carrinho de Compra</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
</head>
<body>
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th>Produto</th>
<th>Quantia</th>
</tr>
</thead>
<tbody id="listaProdutos">
<pd
v-for="item in produtos"
v-bind:key="item.id"
v-bind:nome="item.nome"
v-bind:quantia="item.quantia"></pd>
</tbody>
</table>
</div>
<script src="./main.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</body>
</html>
Hi Luiz, can you explain why it is that involving a
<template>
already works?– Sergio
improved the explanation.
– Guto
Valew, I was struggling, I tried to use the
is
but was using it in the wrong way. Thank you for the answer.– Lucas Emanuel