在 Vue3 中使用过滤器但无法读取 globalProperties

Use filter in Vue3 but can't read globalProperties

只是一个简单的问题,

我知道 Vue3 不再使用过滤器并且注释说使用 computed 或 methd 代替。还有一个我们可以使用的 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');

我在 table 中使用过滤器,就像这样

    <td>
         {{ $filters.formatDate(incident.incidentData) }}
    </td>

配置字段属于根实例而不属于根组件所以你应该这样做:

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) return 根实例

myApp.mount('#app'); 将根应用程序安装到元素后,它 return 是根组件