1

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.

2 Answers 2

4

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;
                }
            }
        })

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

Comments

2

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.

Comments

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.