首页 > 解决方案 > 将碳日期作为道具传递将在 vue 模板中返回计算

问题描述

我想问一下你是怎么解决这个问题的

:birthdate="{{ $user->profile->birthdate->format('m-d-Y') }}"

在我的模型中我有这个

/**
    * The attributes that should be cast to native types.
    *
    * @var array
    *//**
    */
    protected $dates = [
        'birthdate',
    ];

我想在我的 vue 模板中将它作为道具传递

props : { 
        birthdate : {   
            type : String, 
            required : true ,
            default : null
        }
    }

但相反,我得到一个取决于道具的计算。只要看看我告诉过你的 :birthdate 道具,如果我把它像 m/d/Y 一样,它会分割整个事物,所以如果它像12/21/2019它会分割它,如果它是就像12-21-2019它在 vue 模板上传递时会减去整个东西

标签: laravelvue.jslaravel-bladephp-carbon

解决方案


尝试删除vue绑定:并将其用作:

birthdate="{{ $user->profile->birthdate->format('m-d-Y') }}"

或保留:但将您的字符串用单引号括起来。

:birthdate="'{{ $user->profile->birthdate->format('m-d-Y') }}'"

或者赋值给一个变量

:birthdate="myVar"

data(){
return{ myVar: "{{ $user->profile->birthdate->format('m-d-Y') }}"};
}

因为:orv-bind需要表达式/变量,所以当您从 PHP{{ $user->profile->birthdate->format('m-d-Y') }}中回显时, 12-21-2019vue 会将减法视为整数而不是字符串。


推荐阅读