简体   繁体   English

在功能组件中将类作为函数 - Vue

[英]Class as function in functional component - Vue

I am writing a simple functional component in vuejs.我正在用 vuejs 编写一个简单的功能组件。 Currently stuck at a situation where I want to add conditional css class based on the props passed to it.目前陷入了一种情况,我想根据传递给它的道具添加条件 css 类。

However the below doesn't work as expected and I am wondering what wrong am I doing here.然而,下面没有按预期工作,我想知道我在这里做错了什么。

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

The class property cannot be a function, it needs to be a string/array/object. class属性不能是函数,它必须是字符串/数组/对象。

Do this instead:改为这样做:

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

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

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