如何仅接受Angular @Input中的预定义值 [英] How to accept only predefined values in an Angular @Input

查看:92
本文介绍了如何仅接受Angular @Input中的预定义值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我在a中收到一个字符串值作为组件的参数,但我想限制可以用作参数的值,就像enum

My problem here is that I receive a string value in a as a parameter to a component, but I want to limit the values that can be used as a parameter, just like an enum

我用

@Input() type: string = '';

但是在组件中,所有内容都可以引入type属性,我需要将其限制为3个选项,就像我之前说的那样,例如Enum

But the in the component, everything can be introduced to the type property and I need to limit that to just 3 options, as I said before, like an Enum

推荐答案

创建 strong>枚举 ,然后将input的类型设置为枚举.如果它是枚举中的值之一,则将传递您的值,否则将为undefined

Create an Enum and set the type of your input to the enum. Your value will be passed if it is one of the values in enum, otherwise it will be undefined

enum MyEnum {
    First,
    Second,
    Third
}

@Input() type: MyEnum;

这篇关于如何仅接受Angular @Input中的预定义值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆