Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a comma-seperated string in a v-for method

I'm trying to split a string on each comma using a v-for method from Vue.js.

Input: tags: 'test1, test2, test3'
Method: <span v-for="tag in tags" v-text="tag"/>
Output: t e s t 1 , t e s t 2 , t e s t 3
Desired output: test1 test2 test3

How could I split the comma-separated text correctly? I cannot access 'tags' input unless I am in a v-for loop in order to call object.tags.

Not sure why this does not work although It's pretty much the same thing in my code: https://jsfiddle.net/9o3o11jc/1/

like image 463
Gordon Freeman Turtle Avatar asked Dec 24 '22 12:12

Gordon Freeman Turtle


1 Answers

You can use String.prototype.split() to turn your string into an array of words.

new Vue({
  el: '#app',
  data() {
    return {objects:[{tags: 'test1, test2, test3'}]}
   },
   methods: {
      splitJoin(theText){
      	return theText.split(', ');
      }
   } 
});
<script src='https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.9/vue.min.js'></script>
<div id="app">
  <div v-for="object in objects">
    <span v-for="tag in splitJoin(object.tags)" v-text="tag"></span>
  </div>
</div>
like image 117
Steven B. Avatar answered Dec 28 '22 22:12

Steven B.