【问题标题】:Presentation Component in Vue2Vue2 中的表示组件
【发布时间】:2017-03-17 04:15:18
【问题描述】:

我想在浮动边栏中显示我的所有表单和信息页面。 我不想将浮动侧边框 html 复制并粘贴到所有地方。所以我想创建一个组件来充当我的表单或信息页面的容器。 这是表单的示例代码。

<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>

在上面的代码中,我使用vue-form-generator 来生成表单。 floating-sidebox 元素对于所有表单和信息页面都是通用的。我想通过 Presentational 组件对其进行抽象。

我该怎么做Vue2

【问题讨论】:

  • 我会使用vue-router 并将上述表单组件作为外部布局的一部分。然后您的页面内容将呈现在&lt;router-view&gt;&lt;/router-view&gt;
  • 同意@Phil。我写了一个简单的幻灯片应用,叫做takahashi,你可以看看我是如何用vue-router实现的。
  • @Phil 谢谢。但就我而言,我无法将其作为单独的路由器。它是一条路线的一部分。

标签: vue.js vuejs2 vue-component


【解决方案1】:

定义一个包含所有“浮动边箱”组件的组件。您可以通过 this.$children 访问“浮动边箱”,并将其标题等用作导航占位符。由于 $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;
    }
}

然后你可以在包装视图的模板中绑定

<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 与组件 https://jsfiddle.net/ptk5ostr/3/

【讨论】:

  • “定义一个包含所有“浮动边箱”组件的组件是什么意思?我还没有这个组件。我很困惑。你能分享一下jsfiddle吗?
  • 我是认真的。我知道您还没有该组件,这就是为什么我已经对您进行了三个采样(如果可能的话)。如果您为我们提供小提琴,我们可以轻松进行更改。我不会自己创造一个完整的小提琴
  • 好的。我将创建小提琴并与您分享。
  • 我不相信我理解正确。我不知何故认为您已经为您的表单提供了一个组件。所以这里有一个简单的复制粘贴方法来将你的浮动侧边箱放在一个组件中。 jsfiddle.net/ptk5ostr/3
  • 太棒了。这个对我有用。我可以在我的主要组件中包含 vue 表单生成器,而不是在浮动边箱组件中包含它。所以我可以在边箱里放任何东西,而不仅仅是表格。它类似于 angularjs 中的trasclude
猜你喜欢
  • 2018-07-03
  • 2020-09-10
  • 1970-01-01
  • 1970-01-01
  • 2019-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-23
相关资源
最近更新 更多