简体   繁体   English

Vue2中的演示组件

[英]Presentation Component in Vue2

I want to display all my forms and info pages in floating sidebox. 我想在浮动边箱中显示我所有的表单和信息页面。 I don't want to copy and paste the floating sidebox html to all the places. 我不想将浮动边箱html复制并粘贴到所有位置。 So I want to create a component which acts as container to my forms or info pages. 因此,我想创建一个充当表单或信息页面容器的组件。 This is the sample code for form. 这是表单的示例代码。

<div class="floating-sidebox">
  <div class="sidebox-header">
    <div class="sidebox-center">
      <h3 class="title">{{ title }}</h3>
    </div>
  </div>
  <div class="sidebox-content">
    <div class="sidebox-center">
      <!-- This is the actual content. Above container code is common for all forms. -->
      <vue-form-generator :schema="schema" :model="model" :options="{}"></vue-form-generator>
    </div>
  </div>
  <div class="floating-sidebox-close" @click="cancel"></div>
</div>
<div class="floating-sidebox-overlay"></div>

In above code, I uses vue-form-generator to generate the form. 在上面的代码中,我使用vue-form-generator生成表单。 The floating-sidebox elements are common for all forms and info pages. floating-sidebox元素对于所有表单和信息页面都是通用的。 I want to abstract it by Presentational component. 我想通过Presentational组件对其进行抽象。

How could I do it Vue2 ? 我该怎么做Vue2

Define a component that wraps all your "floating-sidebox" components. 定义一个包装所有“浮动边箱”组件的组件。 You can access the "floating-sideboxes" via this.$children and use their title etc. as navigation placeholder. 您可以通过this。$ children访问“浮动边箱”,并将其标题等用作导航占位符。 Since $children is an array you can easily represent the currently visible entity with and index 由于$ children是一个数组,因此您可以轻松地用和索引表示当前可见的实体

...
data: function() {
   sideboxes: [],
   currentIndex: null
},
...
mounted: function() {
   this.sideboxes = this.$children;
   this.currentIndex= this.$children.length > 0 ? 0 : null;
},
...
computed: {
    current: function() {
       return this.currentIndex ? this.sideboxes[this.currentIndex] : null;
    }
}

You can then bind in the template of the wrapping view 然后,您可以在包装视图的模板中进行绑定

<ul>
    <li v-for="(sidebox, index) in sideboxes" @click="currentIndex = index"><!-- bind to prop like title here --></li>
</ul>

<div>
   <!-- binding against current -->
</div>

JSfiddle with component https://jsfiddle.net/ptk5ostr/3/ JSfiddle与组件https://jsfiddle.net/ptk5ostr/3/

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

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