1

Can someone please let me know if there is a way to have two result() in Vue computed based on element id? For example I want return into #and a result which is replaced comma with AND and an other result for #dash which in this result is replaced with -

enter image description here

new Vue({
  el: '#demo',
  data() {
    return {
      target: '',
    }
  },
  computed: {
    result() {
      //if it is going to #and
      // return '`' + this.target.replaceAll(",", "` AND `") + '`' // If {{ result }} is #and
      // if it is going to #dash
      return '`' + this.target.replaceAll(",", "` - `") + '`' // If {{ result }} is #dash
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <p id="and">You Entered {{ result }}</p>
  <p id="dash">You Entered {{ result }}</p>
  <input v-model="target" placeholder="Enter Your Target" />
</div>

1 Answer 1

1

You can create a function instead:

new Vue({
  el: '#demo',
  data() { return { target: '' } },
  methods: {
    getSeperatedInputVal(sep) {
      return this.target && 
        this.target.split(',').map(str => '`'+ str + '`').join(sep);
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <p id="and">You Entered {{ getSeperatedInputVal(" AND ") }}</p>
  <p id="dash">You Entered {{ getSeperatedInputVal(" - ") }}</p>
  <input v-model="target" placeholder="Enter Your Target" />
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Majed, this is what I was lookin for, but there is one minor issue here. as you can see the outputs look like 'rome AND london AND rome' but they should be 'rome' AND 'london' AND 'rome'
@Suffii you're welcome, check now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.