[我正在使用最新的 vue 2.x、vuex 和 vue-cli 3]
我正尝试在 Vue 过滤器中使用 Mixin 来转换这个字符串
/Date(1546329600000)/
我从服务器获取到 java 脚本日期(以下代码在我的 main.js 文件中,就在我创建 Vue 实例之前):
// Mixins:
Vue.mixin({
methods: {
parseDateTicks: str => str.replace("/Date(", "").replace(")/", "")
}
});
// Filters:
Vue.filter('dateTime', (date) => {
if(date){
//date = date.replace("/Date(", "").replace(")/", "");
date = this.parseDateTicks(date);
return new Date(+date).toLocaleString();
}
else{
return '';
}
});
不起作用的部分是
date = this.parseDateTicks(date);
我收到以下错误:
Error in render: "TypeError: Unable to get property 'parseDateTicks' of undefined or null reference"
这在 Vue.js 中是不可能的吗?
最佳答案
您无法在过滤器中访问它。过滤器应该是纯函数。 Source
关于javascript - 在 Vue.Filter 中使用 Vue.Mixin 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51411056/