繁体   English   中英

VueJS:渲染组件的动态模板

[英]VueJS: Render dynamic template for component

我的父页面上有一个组件,如下所示:

Detail.vue

模板:

<feature-list :description="description"></feature-list>

脚本:

axios.get(`my/end/point`)
.then(res => {
  this.description = res.data.product.description
 })
 .catch(function (error) {
   console.log('error', error)
 })

Features.vue

模板:

<component :is="subcomponent" :height="overallHeight" :width="overallWidth" :length="overallLength"></component>

脚本:

components: {
  subcomponent: {
    props: ['overallHeight', 'overallWidth', 'overallLength'],
    /* template: this.description */
  }
}

来自this.description预期返回值:

  <el-row :gutter="30">
      <el-col :sm="24">
        <ul class="dimensions d-flex">
          <li class="dimension">
            <img src="/length.jpg" alt="">
            <h4 class="title">Length</h4>
            <p class="base-copy">{{length}}</p>
          </li>
          <li class="dimension">
            <img src="/height.jpg" alt="">
            <h4 class="title">Height</h4>
            <p class="base-copy">{{height}}</p>
          </li>
          <li class="dimension">
            <img src="/width.jpg" alt="">
            <h4 class="title">Width</h4>
            <p class="base-copy">{{width}}</p>
          </li>
        </ul>
      </el-col>
    </el-row>

如何将模板发送到组件? 并渲染值(长度,宽度和高度)

这样的东西,唯一的区别是,模板来自ajax调用。

经过一番挖掘之后,我找到了解决此问题的方法,其想法是将templateprops作为对象传递给is prop。

该代码将如下所示,

<component 
 :is="description && { template: `<div>${description}</div>`, props: ['height', 'width', 'length'] }" 
 :height="overallheight" 
 :width="overallwidth" 
 :length="overalllength">
</component>

暂无
暂无

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

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