Home > OS >  use a parameter from typescript in template?
use a parameter from typescript in template?

Time:07-02

I have the following a computed property to check wether a user is more active of on social media or on social professional network

mediaOrSocial() {
  if (this.socialMedia) {
    const postLikes = this.socialMedia.postLikes;
    return postLikes === 30 || this.postLikes !== 30;
  } else if (this.socialPro) {
    const postLikes = this.socialPro.postLikes;
    return postLikes === 30 || this.postLikes !== 30;
  }
},

my question is can I borrow the variable postLikes in template to make a condition on string interporlation in template {{ }} ?

exp {{ postLikes > 100 ? 'influencer' : 'not enough visibility' }}

CodePudding user response:

If you mean postLikes variable that you are defining in the mediaOrSocial() function (not the this.postLikes variable) then no - you can't use it in the template. But you can create another computed property which will return the correct value.

numberOfPostLikesForSpecificPlatform() {
  if (this.socialMedia) {
    return this.socialMedia.postLikes;
  } else if (this.socialPro) {
    return this.socialPro.postLikes;
  }
  return null
}

then in the template:

{{ numberOfPostLikesForSpecificPlatform > 100 ? 'influencer' : 'not enough visibility' }}

Remember also to always return some value in the computed property:

mediaOrSocial() {
  if (this.socialMedia) {
    const postLikes = this.socialMedia.postLikes;
    return postLikes === 30 || this.postLikes !== 30;
  } else if (this.socialPro) {
    const postLikes = this.socialPro.postLikes;
    return postLikes === 30 || this.postLikes !== 30;
  }
  // or some other value
  return false
},
  • Related