gpt4 book ai didi

twitter-bootstrap - Modal 中的 Vue.js AJAX 调用

转载 作者:搜寻专家 更新时间:2023-10-30 22:09:23 24 4
gpt4 key购买 nike

我正在尝试弹出一个包含信用卡详细信息的模式。详细信息来自 AJAX 请求。由于某种原因,根 Vue 实例正在更新,但组件实例没有。这就是我目前拥有的 -

HTML:

<!-- View Card Details Modal -->
<!-- Modal -->
<div class="modal fade" id="ccdetails" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">View Card Details</h4>
</div>
<card-details cardid="{{$cc->card_id}}" :message="getCCDetails('{{$cc->card_id}}')"></card-details>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<!-- <button type="button" class="btn btn-primary">Save changes</button> -->
</div>
</div>
</div>
</div>

VueJS:

<script type="text/javascript">
Vue.component('card-details', {
template: '<div class="modal-body">@{{message}}</div>',
// data is technically a function, so Vue won't
// complain, but we return the same object
// reference for each component instance
props: ['message', 'cardid']
}),
new Vue({
el: '#ccdetails',
data: {
cardid: '',
message: ''
},
methods: {
getCCDetails: function (id) {
console.log(id)
console.log('calling function')
axios.get('/card/'.concat(id))
.then(function (response) {
this.message = JSON.stringify(response.data)
}.bind(this))
.catch(function (error) {
return this.message = 'Sorry there was an error'
}.bind(this));
}
}
})
</script>

对于输出,Root 实例具有我想要的 cardid = undefinedmessage = the output。我的 cardDetails 实例的 cardid 值正确,但 message = undefined

最佳答案

你可以试试事件

在组件中添加事件监听器:

Vue.component('card-details', {
template: '<div class="modal-body">@{{message}}</div>',
// data is technically a function, so Vue won't
// complain, but we return the same object
// reference for each component instance
props: ['cardid'],

data: {
message: ''
},

mounted() {
this.$parent.$on("card-details:message", message => {
this.message = message;
});
},
}),

在函数中添加发射线:

    getCCDetails: function (id) {
console.log(id)
console.log('calling function')
axios.get('/card/'.concat(id))
.then(function (response) {
this.message = JSON.stringify(response.data)
this.$emit('card-details:message', this.message) // <--
}.bind(this))
.catch(function (error) {
return this.message = 'Sorry there was an error'
}.bind(this));

}

并使 getCCDetails 函数仅在 View Details button click 时调用。

关于twitter-bootstrap - Modal 中的 Vue.js AJAX 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43794243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com