[英]how do i use vue.js to display API array as bootstrap cards using axios
I Dont really understand Vue that well yet but i am trying to get a card display of my products from my API i created.我还不太了解 Vue,但我正在尝试从我创建的 API 中获取我的产品的卡片展示。 i managed to display everything in php code by using a while loop and that worked great but i want to try it with axios and vue.我设法通过使用 while 循环显示 php 代码中的所有内容,效果很好,但我想尝试使用 axios 和 vue。 my array keys are in the html code but i think i am calling them wrong aswell as not understanding the logic to create a while loop.我的数组键在 html 代码中,但我认为我调用它们是错误的,也不理解创建 while 循环的逻辑。 ill post the php code to show what i am trying to do with vue and axios at the end of this post.我会在本文末尾发布 php 代码来展示我正在尝试使用 Vue 和 axios 做什么。 app is just to show that my api is working, app2 is the code to get the display. app 只是为了显示我的 api 正在工作,app2 是获取显示的代码。
<!-- vue while loop of products from API in bootstrap cards -->
<section>
<div class="row">
<div class="col-md-12">
<div class="row">
<!-- vue while loop of info array -->
<div id="app2" >
<div class="col-md-3">
<div class="card">
<div class="card-body">
<!-- while loop goes here, i think -->
<div class="product" v-while='info2 in data'>
<img src={{productID.productImage}} alt="">
<h4 class="text-info">{{productID.productName}}</h4>
<h4 class="text-muted">{{productID.price}} Coins</h4>
<p class="text-muted">{{productID.productDescription}}</p>
<input type="number" name="quantity" class="form-control" value="1">
<input type="hidden" name="productName" value="{{productID}}">
<input type="hidden" name="price" value="{{productID.price}}">
<input type="submit" name="add_to_cart" class="btn btn-primary btn-block fa-lg gradient-custom-2 mb-3" value="Add to Cart">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<!-- temporarily display json data, this is showing an array from my API -->
<div id="app">
{{ info }}
</div>
<!-- vue code for product display -->
<script>
var app2 = new Vue({
el: '#app2',
data: {
info2: '',
},
methods: {
getInfo2: function () {
axios.get('http://localhost:8888/DevsDev2022/productAPI/products')
.then(function (response) {
app2.info2 = response.data;
})
.catch(function (error) {
console.log(error);
});
}
},
mounted: function () {
this.getInfo2();
}
});
</script>
<!-- vue code to temporarily display jsan data -->
<script>
var app = new Vue({
el: '#app',
data: {
info: '',
},
methods: {
getInfo: function () {
axios.get('http://localhost:8888/DevsDev2022/productAPI/products')
.then(function (response) {
app.info = response.data;
})
.catch(function (error) {
console.log(error);
});
}
},
mounted: function () {
this.getInfo();
}
});
</script>
PHP code that works PHP 有效代码
<section>
<div class="row">
<div class="col-md-12">
<div class="row">
<?php
$query = 'SELECT * FROM products ORDER BY productID ASC';
$result = mysqli_query($conn,$query);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
echo '
<div class="col-md-3">
<div class="card">
<div class="card-body">
<form method="post" action="index.php?action=add&id='.$row['productID'].'">
<div class="product">
<img src="'.$row['productImage'].'" alt="">
<h4 class="text-info">'.$row['productName'].'</h4>
<h4 class="text-muted">'.$row['price'].' Coins</h4>
<p class="text-muted">'.$row['productDescription'].'</p>
<input type="number" name="quantity" class="form-control" value="1">
<input type="hidden" name="productName" value="'.$row['productID'].'">
<input type="hidden" name="price" value="'.$row['price'].'">
<input type="submit" name="add_to_cart" class="btn btn-primary btn-block fa-lg gradient-custom-2 mb-3" value="Add to Cart">
</div>
</form>
</div>
</div>
</div>
';
}
}
?>
</div>
</div>
</div>
</section>
it should be something like this.它应该是这样的。 There's no such a directive as v-while
.没有v-while
这样的指令。 You use v-for
if you wanna render a list如果你想渲染一个列表,你可以使用v-for
<div id="app2" >
<div class="col-md-3">
<div class="card">
<div class="card-body">
<!-- while loop goes here, i think -->
<div class="product" v-for="product in info2" :key="product.id">
<img :src="product.productImage" alt="">
<h4 class="text-info">{{ product.productName }}</h4>
<h4 class="text-muted">{{ product.price }} Coins</h4>
<p class="text-muted">{{ product.productDescription }}</p>
<input type="number" name="quantity" class="form-control" value="1">
<input type="hidden" name="productName" value="product.productName">
<input type="hidden" name="price" value="product.price">
<input type="submit" name="add_to_cart" class="btn btn-primary btn-block fa-lg gradient-custom-2 mb-3" value="Add to Cart">
</div>
</div>
</div>
</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.