我在.vue组件文件中有一个模板。假设我需要重用此模板的一部分(<img :bind1 :bind2 :bind3),我需要这样的东西:

<a v-if=...><img></a>
<img v-else>


Img与此处的代码相同。
最好的方法是什么?

最佳答案

如果需要重复重新创建相同的代码段,最好的方法是使用render function to create a functional component。这是一个简单的例子:

Vue.component('my-img', {
  functional: true,
  render: function (createElement, context) {
    return createElement('img', { attrs: { src: context.props.src } })
  },
  props: {
    src: 'http://example.com/img.png'
  }
})

关于javascript - Vue-部分模板,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49334820/

10-14 18:38
查看更多