简体   繁体   English

如何在 Vue.Js 中将 prop 中的变量设置为默认值?

[英]How to set as default value a variable in a prop in Vue.Js?

I have a question.我有个问题。 I need to set a prop with a default value that is obtained from a getter.我需要使用从 getter 获得的默认值设置一个道具。

props: {
    userID: {
      type: String,
      default: ''
    }
  }

The value that I want to set as default value is obtained with:我想设置为默认值的值是通过以下方式获得的:

computed: {
    ...mapGetters('Auth', ['getId']),

}

How can I make the prop UserID have as default value the getId that is obtained from the mapGetter?如何使道具UserID具有从 mapGetter 获得的getId作为默认值?

You can use computed property for setting the default value.您可以使用computed属性来设置默认值。

computed: {
  userIdComp() {
    return this.userID ?? this.$store.getters['Auth/getId'];
  }
}

And use userIdComp instead of userID并使用userIdComp而不是userID

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

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