vue如何让自定义函数挂到全局

在main.js里进行全局注册

Vue.prototype.ajax = function (){}

在所有组件里可调用

this.ajax
实例main.js
Vue. prototype. ChangeDateFormat = function ( row, column){
// ChangeDateFormat(row, column) {
// var jsondate = row[column.property];
var jsondate = row[ column. property];
jsondate = jsondate. replace( "/Date(", ""). replace( ")/", "");
if ( jsondate. indexOf( "+") > 0) {
jsondate = jsondate. substring( 0, jsondate. indexOf( "+"));
} else if ( jsondate. indexOf( "-") > 0) {
jsondate = jsondate. substring( 0, jsondate. indexOf( "-"));
}
var date = new Date( parseInt( jsondate, 10));
var month = date. getMonth() + 1 < 10 ? "0" + ( date. getMonth() + 1) : date. getMonth() + 1;
var currentDate = date. getDate() < 10 ? "0" + date. getDate() : date. getDate();
var hour = date. getHours() < 10 ? "0" + date. getHours() : date. getHours();
var minutes = date. getMinutes() < 10 ? "0" + date. getMinutes() : date. getMinutes();
var second = date. getSeconds() < 10 ? "0" + date. getSeconds() : date. getSeconds();
return date. getFullYear() + "-" + month + "-" + currentDate + " " + hour + ":" + minutes + ":" + second;
}
index.vue
< el-table-column prop= "last_time" :formatter=" this. ChangeDateFormat" width= "200px" align= "center" label= "登陆时间" > </ el-table-column >
Vue 3中,可以使用Composition API来编写自定义函数。Composition API是Vue 3中引入的一种新的API风格,它允许我们将逻辑组织为可重用的函数。 下面是Vue 3中自定义函数的写法: 1. 使用`setup`函数:在组件中使用`setup`函数来定义自定义函数。`setup`函数是一个特殊的函数,它会在组件实例创建之前执行,并且可以返回一个对象,该对象中包含了我们需要暴露给模板使用的函数。 2. 定义自定义函数:在`setup`函数中,我们可以定义我们的自定义函数。这些函数可以是普通的JavaScript函数,也可以是响应式的函数。 3. 返回暴露的函数:在`setup`函数中,我们需要将我们定义的自定义函数返回给模板使用。可以通过返回一个对象,将自定义函数作为对象的属性进行返回。 下面是一个简单的示例: ```javascript <template> <div> <button @click="increment">Increment</button> <p>Count: {{ count }}</p> </div> </template> <script> import { ref } from 'vue'; export default { setup() { // 定义一个响应式的count变量 const count = ref(0); // 定义自定义函数increment function increment() { count.value++; } // 返回暴露的函数 return { count, increment }; } }; </script> ``` 在上面的示例中,我们使用`ref`函数创建了一个响应式的`count`变量,并定义了一个自定义函数`increment`来增加`count`的值。然后,我们将`count`和`increment`函数通过返回一个对象的方式暴露给模板使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蔡俊锋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值