【问题标题】:Vue js load modal content with ajaxVue js 使用 ajax 加载模态内容
【发布时间】:2017-03-25 00:52:27
【问题描述】:

我在 laravel 中使用 vue js,在我的页面中我有 13 个模态,我认为在同一页面中插入 13 个模态不是一个好主意,所以我把它放在我的刀片中:

<modal v-if="showModal" @close="showModal = false">
     <header slot="header" v-html="modalHeader"></header>
     <component slot="body" :is="currentBody"></component>
</modal>

在我的 file.js 中有这个:

Vue.component('modal', require('./components/Modal.vue'));
Vue.component('StatusModal', require('./components/modals/StatusModal.vue'));
Vue.component('UserModal', require('./components/modals/UserModal.vue'));

const profile = new Vue({
    el: '#profile',
    data: {
        showModal: false,
        modalHeader: '',
        currentBody: '',
    },
    methods: {
        showStatus(){
            this.showModal = true
            this.modalHeader = 'Confirmation'
            this.currentBody = 'StatusModal'

        },
        showUser(){
            this.showModal = true
            this.modalHeader = 'Confirmation'
            this.currentBody = 'UserModal'
        }
    }
})

例如,这里我有两个模态“StatusModal”和“UserModal”,我将它们加载到我的 js 文件中,所以如果我有 13 个或更多模态,我将在我的 js 文件中加载 13 个组件,我需要一个解决方案调用函数时只加载我需要的组件,函数调用时可以加载组件吗?

我尝试使用 ajax 并在 ajax 调用中加载刀片内容并将其加载到我的模式中,但我遇到了一个问题,我正在对输入使用 vue 验证,因此验证不起作用(vee-validate)以及任何 vue 变量没有编译,我在我的模式中得到 {{ variable }}。

什么是我的案例的最佳解决方案?感谢您的帮助

【问题讨论】:

  • 你在使用 vue-cli webpack 配置吗?

标签: javascript php ajax laravel-5 vuejs2


【解决方案1】:

聊天后编辑

找到解决方案:

HTML:

<component slot="body" :is="currentBody"></component>

JS:

showStatus() { 
  this.dialog = true 
  System.import('./components/modals/StatusModal.vue').then((response) => { 
    this.currentBody = response
  }); 
},

上一个答案

如果使用 vue-cli webpack config(modules),我会寻求如下解决方案。关键是使用System.import。它与 require 相同,但异步。即使您多次调用同一个文件,它也只会解析一次。它会在客户端缓存它。

我在这里使用render function,因为管理vue要容易得多模板替换

import Modal from './components/Modal.vue'

const profile = new Vue({
  render(h) {
    if (this.showModal) {
      return h('modal', {
        on: {
          close() {
            this.showModal = false
          }
        }
      }, [
        h('header', { slot: 'header' }, this.modalHeader),
        h(this.currentBody, { slot: 'body' })
      ]);
    } else {
      return null;
    }
  },
  data: {
    showModal: false,
    modalHeader: '',
    currentBody: {},
  },
  methods: {
    showStatus() {
      this.showModal = true
      this.modalHeader = 'Confirmation'
      this.currentBody = System.import('./components/modals/StatusModal.vue')
    },
    showUser() {
      this.showModal = true
      this.modalHeader = 'Confirmation'
      this.currentBody = System.import('./components/modals/UserModal.vue')
    }
  },
  components: {
    Modal
  }
})

注意我没有测试过。如果有任何问题或我误解了您的情况,请告诉我。

【讨论】:

  • 我会测试它,我会通知你,如果我运行 showStatus 函数并导入 StatusModal 组件,并且在我运行 showUser 函数并导入 UserModal 组件后,我将有两个导入?这对性能不利?
  • 在这种情况下,您将有 2 个负载。这对性能没什么不好。您只需轻轻一点(显示页面),然后在运行时加载您需要的内容。这比一开始就加载可能需要的所有东西要好得多。这将使您的用户在页面之间等待更多。他们可能正在加载无用的组件/数据。
  • 你没有提到 el: "#app" 吗?
  • 你也没有,我使用了你的模板。没有el 选项,因为模板在render 函数中。所以不需要指向 DOM 中的一个元素来定义组件的模板。
  • 当我删除 el: "#app" 时,我什么也没编译
猜你喜欢
  • 1970-01-01
  • 2012-08-11
  • 2018-02-20
  • 1970-01-01
  • 1970-01-01
  • 2017-09-15
  • 2011-04-30
  • 1970-01-01
  • 2011-04-19
相关资源
最近更新 更多