Vue.js显示从另一个组件调用一个组件

时间:2017-04-03 19:54:22

标签: javascript vue.js vuejs2 vue-component

我有两个组成部分:

Vue.component('repo-button', {
  props:["check_in_id", "repo_id"],
  template: '#repo-button',
  methods: {
    fetchRepo: function() {
        url = window.location.href.split("#")[0] + "/check_ins/" + this.check_in_id + "/repositionings/" + this.repo_id + ".json"
        cl(url)
        cl(this)
        var that;
        that = this;

        $.ajax({
            url: url,
            success: function(data) {
                cl(data)
                that.showRepo();
            }
        })

    },
    showRepo: function() {
        // what do I put here to display the modal 
    }
  },
  data: function() {
  var that = this;
  return {

  }
}
});

Vue.component('repo-modal', {
    template: "#repo-modal",
    data: function() {
        return {
            status: 'none'
        }
    }
});

var repositionings = new Vue({
    el: "#repo-vue"
});

...我的观点包括一个按钮和一个模态。我想在repo-button组件上调用fetchRepo按钮并显示模态(将status属性从none更改为block

<script type="text/x-template" id="repo-button">
    <div class='socialCircle-item success'>
    <i class='fa fa-comment' 
         @click="fetchRepo"
      :data-check_in='check_in_id' 
      :data-repo='repo_id'> 
    </i>
  </div>
</script>

<script type="text/x-template" id="repo-modal">
    <div  v-bind:style="{ display: status }" class="modal" id="vue-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-client_id="<%= @client.id %>">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title"></h4>
                </div>
                <div class="modal-body"></div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger btn-simple" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
</script>

<div id="repo-vue">
    <div is="repo-modal"></div>
    <div is="repo-button" repo_id="<%= ci.repositioning.id %>" check_in_id="<%= ci.id %>"></div>
</div>

1 个答案:

答案 0 :(得分:5)

Props down, events up

  

在Vue.js中,可以汇总父子组件关系   作为道具,事件起来。父母将数据传递给孩子   道具,孩子通过事件向父母发送消息。

特别是,如果组件的状态需要在外部(由父级或兄弟级)进行控制,则该状态应作为父级的prop传递。事件向父母表明应该改变状态。

你的模态的状态由事件本身和兄弟组件控制。因此,州居住在父母身边,并作为道具传递给模态。单击模态“关闭”按钮会发出(自定义)hidemodal事件;单击兄弟组件的评论图标会发出showmodal个事件。父级通过相应地设置其showRepoModal数据项来处理这些事件。

Vue.component('repo-button', {
  template: '#repo-button',
  methods: {
    showRepo: function() {
      this.$emit('showmodal');
    }
  }
});

Vue.component('repo-modal', {
  template: "#repo-modal",
  props: ["show"],
  computed: {
    status() {
      return this.show ? 'block' : 'none'
    }
  },
  methods: {
    hideRepo() {
      this.$emit('hidemodal');
    }
  }
});

var repositionings = new Vue({
  el: "#repo-vue",
  data: {
    showRepoModal: false
  }
});
.socialCircle-item i {
  cursor: pointer;
}
<link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.2.6/vue.min.js"></script>
<template id="repo-button">
    <div class='socialCircle-item success'>
    <i class='fa fa-comment' 
         @click="showRepo"
         >
    </i>
  </div>
</template>

<template id="repo-modal">
    <div  v-bind:style="{ display: status }" class="modal" id="vue-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" >
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title"></h4>
                </div>
                <div class="modal-body"></div>
                <div class="modal-footer">
                    <button type="button" @click="hideRepo" class="btn btn-danger btn-simple" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
</template>

<div id="repo-vue">
  <div is="repo-modal" :show="showRepoModal" @hidemodal="showRepoModal = false"></div>
  <div is="repo-button" @showmodal="showRepoModal = true"></div>
</div>