gpt4 book ai didi

vue.js - 在 Vue3 中使用过滤器但无法读取 globalProperties

转载 作者:行者123 更新时间:2023-12-04 08:21:55 28 4
gpt4 key购买 nike

只是一个快速的问题,
我知道 Vue3 不再使用过滤器,注释说使用计算或方法代替。但还有一个我们可以使用的 globalProperties,
我使用了这个 globalProperties 但一直收到这个错误
未捕获的类型错误:无法读取未定义的属性“globalProperties”
有谁知道我的代码中的错误在哪里?

const app = {
data() {
return {
message: ""
}
}
}

app.config.globalProperties.$filters = {
formatDate(value) {

if (value == "0001-01-01T00:00:00")
return "";
var today = new Date(value);
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();

today = dd + '/' + mm + '/' + yyyy;
return today;
}
}

Vue.createApp(app).mount('#app');
我像这样在我的表格中使用过滤器
    <td>
{{ $filters.formatDate(incident.incidentData) }}
</td>

最佳答案

config 字段属于 根实例 不给根组件所以你应该这样做:

const app = {
data() {
return {
message: ""
}
}
}
const myApp=Vue.createApp(app)

myApp.config.globalProperties.$filters = {
formatDate(value) {

if (value == "0001-01-01T00:00:00")
return "";
var today = new Date(value);
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();

today = dd + '/' + mm + '/' + yyyy;
return today;
}
}

myApp.mount('#app');
Vue.createApp(app) 返回根实例
myApp.mount('#app'); 将根应用程序安装到元素后,它返回根组件

关于vue.js - 在 Vue3 中使用过滤器但无法读取 globalProperties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65455290/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com