【问题标题】:Class as function in functional component - Vue类作为功能组件中的函数 - Vue
【发布时间】:2021-07-30 10:53:45
【问题描述】:

我正在用 vuejs 编写一个简单的功能组件。目前陷入我想根据传递给它的道具添加条件css类的情况。

但是,以下内容无法按预期工作,我想知道我在这里做错了什么。

<script>
export default {
  name: "BasePill",

  functional: true,

  props: {
    variant: String
  },

  render(createElement, { children, props }) {

    const componentData = {
      staticClass: "text-sm text-center"
      class: function() {
        if (props.variant === 'secondary') {
           return 'bg-secondary'
        }

        return 'bg-primary'
      }
    };

    return createElement("span", componentData, children);
  },
};
</script>

【问题讨论】:

    标签: javascript vue.js vuejs2 vue-component


    【解决方案1】:

    class 属性不能是函数,它必须是字符串/数组/对象。

    改为这样做:

    const componentData = {
      staticClass: 'text-sm text-center',
      class: props.variant === 'secondary' ? 'bg-secondary' : 'bg-primary',
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-09
      • 2020-11-24
      • 1970-01-01
      • 1970-01-01
      • 2020-01-07
      • 2019-05-08
      • 2021-03-22
      • 1970-01-01
      相关资源
      最近更新 更多