【发布时间】:2019-12-22 19:36:37
【问题描述】:
我是 Vue 新手,正在尝试将 axios 获取的数据显示到模态。
问题是模态不显示数据。
我检查了生日变量,它确实包含数据。
模板
<div class="modal fade" tabindex="-1" id="birthdayModal" role="dialog">
<div class="modal-dialog modal-md modal-dialog-centered" role="document">
<div class="modal-content mx-auto text-center bg-danger text-warning">
<div class="modal-body">
<h1>
HAPPY
<i class="fa-fw fas fa-birthday-cake"></i> BIRTHDAY!
</h1>
<img
:src="'/img/members/' +birthday.dob"
width="250px"
height="250px"
class="img img-responsive"
/>
<h3 class="mt-3">{{birthday.alias_name}}</h3>
<h3>{{birthday.dob}}</h3>
</div>
</div>
</div>
</div>
脚本
<script>
export default {
data() {
return {
birthday: [],
}
},
methods: {
birthdayModal() {
axios
.get('api/members/birthday')
.then(res => (this.birthday = res.data))
.then($('#birthdayModal').modal('show'))
console.log('Birthday Data: ', this.birthday)
},
},
created() {},
mounted() {
this.birthdayModal()
console.log('Component mounted.')
},
}
</script>
控制器
public function birthday()
{
$date = Carbon::now();
$member = Member::whereMonth('dob', '=', $date->month)->whereDay('dob', '=', $date->day)->get();
return $member;
}
【问题讨论】:
-
1.你不需要两个
.then()链,先在里面做东西就足够了。 2.尝试在模态体上放置v-if='birthday.length >0'。 3.不建议将jQuery与vue混合使用。