简体   繁体   English

如何使用 Vue.js 模板模态并重用它?

[英]How to use Vue.js to template a Modal and reuse it?

So currently I have a web app that loads a dashboard based on a SQL database and that works just fine, each of these elements have buttons and those buttons are SUPPOSED to spawn a modal with the content of the problem and you can interact with it etc.所以目前我有一个 web 应用程序,它加载基于 SQL 数据库的仪表板并且工作得很好,这些元素中的每一个都有按钮,这些按钮应该产生一个包含问题内容的模式,你可以与它进行交互等.

Recently I have figured out how to use Vue.js to run a function that grabs this information and populate it on the modal.最近我想出了如何使用 Vue.js 来运行一个 function 来获取这些信息并将其填充到模态中。 However, this modal can only be templated ONCE before it wont work again.但是,此模式只能模板化一次,然后才能再次工作。 Ex: I can click on problem 1 and problem 1's information is correct.例如:我可以点击问题 1 并且问题 1 的信息是正确的。 however, if I click problem 2 after that it doesn't work但是,如果我点击问题 2 之后它不起作用

new Vue({
    delimiters: ['!!!','!!!'],
    el: '#problem-modal',
    methods: {
        close() {
            this.$emit('close');
            },
        },

    data: {
        items: [],
        name: null,
        summary: null,
    },
    mounted: function () {
        var self = this;
        fetch('./api/problems')
            .then(response => {
                var response = response.json();
                return response;
            })
            .then(json => {
                var data = json.data;
                var length = data.length;
                for (var i = 0; i<length; i++) {
                    if (data[i].unique_id == button_id) {
                        this.name = data[i].problem_name;
                        this.summary = data[i].summary;
                    }
                }
            })
    }
})
}

Here is my Script for the vue, assume that the problems load perfectly and this:这是我的 vue 脚本,假设问题加载完美,并且:

<div class="modal fade" id="problem-modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">!!! name !!!</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        !!! summary !!!
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

is the modal.是模态的。 Any idea on how to make the modal element able to be used more than once?关于如何使模态元素能够多次使用的任何想法?

Thanks!谢谢!

I give you nearest illustration of your question !我给你最接近你的问题的例子! I thought items is your problem list.我认为items是您的问题清单。 As you mentioned you have to clicked button so that pass your clicked data to call method and grab data from items with that pass clicked data and set vue data.正如您所提到的,您必须单击按钮,以便将单击的数据传递给调用方法,并从具有该传递单击数据的items中获取数据并设置 vue 数据。 I hope it will be ok.. Look at my example我希望它会好起来的..看看我的例子

 new Vue({ el: '#problem-modal', methods: { show(id) { let cur = this.items.find(x => x.id == id); this.name = cur.name; this.summary = cur.summary; } }, data: { items: [ { id: 1, name: "one", summary: "one summary " }, { id: 2, name: "two", summary: "two summary" } ], name: null, summary: null, } });
 button { width: 100%; height: 20px; margin: 10px 5px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="problem-modal"> <button v-for="item in items" @click="show(item.id)">{{item.id}}</button> <div class="modal"> <h1>{{name}}</h1> <pre>{{summary}}</pre> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM