Vue.js AJAX 在模态框内调用

Vue.js AJAX call inside Modal

我正在尝试弹出一个包含信用卡详细信息的模式。详细信息来自 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>

Vue JS:

<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

我在 phone 抱歉,试试这个:

  • 在您的卡片详情中设置:message="message"

  • 然后在根Vue元素中,添加一个:cardid="{{$cc->card_id}}",别忘了cardid prop

  • 然后在根实例中实现 mounted 方法并从那里调用 getCCDetails(this.cardid)

  • 里面getCCDetails设置message属性

希望对您有所帮助

你可以试试活动

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

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));

    }

并且仅在 View Details 按钮单击时调用 getCCDetails 函数

模态不适用于 Ajax 呼叫。 您必须有来自控制器的 Ajax 个呼叫。