Vuejs2 结合双值的动态变量+;滤波器
我有以下情况:Vuejs2 结合双值的动态变量+;滤波器,vuejs2,vue-component,vue-filter,Vuejs2,Vue Component,Vue Filter,我有以下情况: 字幕需要通过组合两个字符串来设置card.beforeChanged包含“last changed to:”字符串,card.changed变量包含一个datetimestring。通过deformatDate()datetimestring将格式化为可读的日期 副标题现在返回:“上次更改为:2069882880” 问题:是否可以将两个字符串合并,其中一个字符串通过filter属性一次性格式化?您应该使用一个字符串 Vue.filter('formatDate',函数(值)
字幕需要通过组合两个字符串来设置card.beforeChanged
包含“last changed to:”字符串,card.changed
变量包含一个datetimestring。通过deformatDate()
datetimestring将格式化为可读的日期
副标题现在返回:“上次更改为:2069882880”
问题:是否可以将两个字符串合并,其中一个字符串通过filter属性一次性格式化?您应该使用一个字符串
Vue.filter('formatDate',函数(值){
返回时刻(值).fromNow()
})
Vue.component('todo'{
计算:{
FormattedText:函数(){
返回`${this.text}-${Vue.filter('formatDate')(this.date)}`
}
},
道具:['text','date'],
模板:“{{formattedtext} ”
});
var demo=新的Vue({
el:'演示',
数据:{
待办事项:[
{文本:'testing',日期:new date()},
{text:'old text',date:moment().subtract(7,'days')}
]
}
})
目前没有todos
通过一种方法让它工作。计算属性不是选项,因为datestring来自模板中的for循环
方法:
formatDate: (date, format) => {
if (!date) return ''
if (!format) format = 'DD.MM.YYYY'
return moment(date).format(format)
}
实施:
<bl-column v-for="(card, index) in cards" :key="card.id">
<bl-card :index="index" type="section" action="drawer" :title="card.titleShort" :subtitle="(card.beforeChanged || '') + ' ' + formatDate(card.changed)" />
</bl-column>
您在示例中所做的有什么问题吗?这不起作用,因为日期来自模板中for循环中的变量。因为计算属性不接受变量,所以这不起作用。