Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vuejs2 结合双值的动态变量+;滤波器_Vuejs2_Vue Component_Vue Filter - Fatal编程技术网

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。通过de
formatDate()
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循环中的变量。因为计算属性不接受变量,所以这不起作用。