lin
lin

Reputation: 221

How could I return value with Vue.js

I has worked with Vue for several days, and meet some problem.

I use jQuery AJAX load data (text content) in template, title and description need to be ellipsis, I wrote the methods

methods:{
               titleELLIPSIS:function(){
                    var title = self.articleList.title;//AJAX data
                    var titleLength = title.length; 
                    var maxWidth = 15;
                    var newTitle = title.split("",maxWidth);
                    return title(function(ELLIPSIS){
                        if(titleLength>maxWidth){
                            for(var j=newTitle.length-1;j>0;j--){
                                delete newTitle[j];
                                var ELLIPSIS = self.innerHTML = newTitle.join('')+'...';
                                if(newTitle.length<=maxWidth){ break;}
                                return ELLIPSIS;
                            }
                        }
                    })

                }
}

And my template is:

<h2 class="ellipsis-2">{{titleELLIPSIS}}</h2>

How could I return the ellipsis title in h2?

Please give me some idea.

By the way, the AJAX is success, because other data show correctly.

Upvotes: 1

Views: 28758

Answers (2)

dvnguyen
dvnguyen

Reputation: 3022

Computed property is what you're looking for. Move titleEllipsis to computed section:

computed: {
   titleELLIPSIS:function(){
        var title = self.articleList.title;//AJAX data
        var titleLength = title.length; 
        var maxWidth = 15;
        var newTitle = title.split("",maxWidth);
        return title(function(ELLIPSIS){
            if(titleLength>maxWidth){
                for(var j=newTitle.length-1;j>0;j--){
                    delete newTitle[j];
                    var ELLIPSIS = self.innerHTML = newTitle.join('')+'...';
                    if(newTitle.length<=maxWidth){ break;}
                    return ELLIPSIS;
                }
            }
        })

    }
}

Upvotes: 4

user320487
user320487

Reputation:

titleELLIPSIS is defined as a method, therefore you need to actually call it.

<h2 class="ellipsis-2">{{ titleELLIPSIS() }}</h2>

The way you are using it is like it's defined as a data or computed property.

Upvotes: 2

Related Questions