vue 循環取值日期格式化,字符串截取處理

用vue取值頁面遍歷時,每次都搜索js日期格式化和字符串長度截取,此次記錄下來,之後從這裏直接拿便可。
html代碼以下javascript

<div id="cold_news">
                        <ul class="col-xs-12">
                            <li class="col-xs-3" v-for="(news, index) in hotNews" >
                                <a href="javascript:void(0)">
                                <img class="pull-left hot_img" :src="news.PicUrl" alt="">
                                <span class="pull-left hot_news">{{news.createdData|formatDate}}</span>
                                <span class="pull-left hot_news" style="width:100px">{{news.title|subString}}</span>
                                </a>
                            </li>
                        </ul>
                    </div>

js代碼以下:html

var app = new Vue({
        el : '#cold_news',
        data : {
            hotNews: [],
        },
        created : function() {
            var _this = this;
            _this.getHotNews();
        },
        updated :function(){
        },
        methods : {
            getHotNews : function() {
                var _this = this;
                $.ajax({
                    type : 'get',
                    url : basePath+ '/jf/bigData/getArticleList',
                    data : {},
                    success : function(res) {
                        if (res.code === 0) {
                            _this.hotNews = res.data;
                        }
                    }
                })
            },
        },
        filters: {
            formatDate(time) {
                var date = new Date(time);
                return formatRiQi(date);
            },
            subString(value) {
                if(value != "" && value.length > 24){
                    return value.substr(0,22) + "…";
                } else {
                    return value;
                }

            }
        }
    });

    // 日期格式化日
    function formatRiQi(sj){
        var now = new Date(sj);
        var   year=now.getFullYear();
        var   month=now.getMonth()+1;
        var   date=now.getDate();
        var   hour=now.getHours();
        var   minute=now.getMinutes();
        var   second=now.getSeconds();
        // return   year+"-"+month+"-"+date; //+"   "+hour+":"+minute+":"+second;
        return   year+"年"+month+"月"+date+"日"; //+"   "+hour+":"+minute+":"+second;

    }

獲取到的json數據格式以下,即hotNews對象:vue

{
	"code": 0,
	"data": [{
		"createdData": "2019-04-10 11:34:05",
		"description": "",
		"url": "",
		"title": "生鮮電商陷入生死劫冷鏈物流不是絆腳石",
		"picUrl": "/images/201904/35d05df7-be79-40ce-bf0d-f1a4e0498f68.jpg"
	}, {
		"createdData": "2019-04-10 11:29:32",
		"description": "",
		"url": "",
		"title": "重磅 | 新規:肉製品企業需對生豬原料自檢非洲豬瘟病毒!5月1日起執行",
		"picUrl": "/images/201904/171b7d8e-7e0b-42b9-bee1-fa0c6d797c11.jpg"
	}, {
		"createdData": "2019-04-10 10:47:34",
		"description": "",
		"url": "",
		"title": "中共中央辦公廳 國務院辦公廳印發《關於促進中小企業健康發展的指導意見》",
		"picUrl": "/images/201904/1b685abb-55f7-4024-9f2e-7a7b240cfa2b.jpg"
	}, {
		"createdData": "2019-04-10 10:45:45",
		"description": "",
		"url": "",
		"title": "特朗普稱將對歐盟商品加徵關稅 歐盟研究報復措施",
		"picUrl": "/images/201904/7057bba9-59f7-41d0-b69b-eed7b4972a37.jpg"
	}]
}

注意js中的處理日期處理調用外部的函數:java

// 日期格式化日
    function formatRiQi(sj){
        var now = new Date(sj);
        var   year=now.getFullYear();
        var   month=now.getMonth()+1;
        var   date=now.getDate();
        var   hour=now.getHours();
        var   minute=now.getMinutes();
        var   second=now.getSeconds();
        // return   year+"-"+month+"-"+date; //+"   "+hour+":"+minute+":"+second;
        return   year+"年"+month+"月"+date+"日"; //+"   "+hour+":"+minute+":"+second;

    }

在vue頁面中使用filters方法:ajax

{{news.title|subString}} 或 {{news.createdData|formatDate}}

最終頁面展現效果:
json

相關文章
相關標籤/搜索