George_Fal 2017-11-02 22:06 采纳率: 0%
浏览 42

Vue v-bind和AJAX

I'm trying to move away from jQuery towards Vue 2.0, which for the most part the transition has felt very easy.

However, when making AJAX calls and interacting with the response I'm approaching it wrong and I can't get into the Vue mindset of doing things.

For example, I have a "Modal trigger" which when clicked opens a modal window and makes an AJAX call to a href which is bound to the trigger and then returns the the html to v-html in the modal. My problem comes when I want to have an @click event in the modal, obviously it doesn't work because Vue hasn't registered it, which makes sense.

In jQuery where I could just say for example $(document).on('click'... so that it could recognise any AJAX loaded pages, I cannot work out a similar option in Vue, I'm clearly approaching the situation wrong.

I've spent hours searching for a solution and I'm no further forward, could anyone with experience of similar situations please let me know how they overcame their issues or point me in the direction of tutorials that deal with similar problems.

Thank you in advance.

  • 写回答

2条回答 默认 最新

  • weixin_33721344 2017-11-03 02:54
    关注

    You need conditional rendering. And the point is, template is not conditional, data is. So the @click is actually always in your template, but with the data changes, you have different views, even different user interaction behaviours in your view.

    • you want a button, but only when some condition flag is true, so in the data option, flag is false initially, but you load the real status from server via an AJAX call, which may set flag to true so the button shows. Template may be like <button v-if="flag" @click="clicked"></button>

    • you want a button, always, but clicking it doesn't always do something(e.g. show an alert), so you have <button @click="clicked"></button>, and if(this.flag) {/*do something*/} in clicked, while flag is loaded by AJAX like above.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog