HarmonyOS鸿蒙Next中如何创建MethodDecorator

发布于 1周前 作者 gougou168 来自 鸿蒙OS

HarmonyOS鸿蒙Next中如何创建MethodDecorator

用ets创建代码:

export function Get(targetUrl: string): MethodDecorator {
  return (target: Object, propertyKey: string, descriptor: PropertyDescriptor) => {
    descriptor.value = () => {
      return targetUrl
    }
  }
}

提示错误:

Type ‘(target: Object, propertyKey: string, descriptor: PropertyDescriptor) => void’ is not assignable to type ‘MethodDecorator’. Types of parameters ‘propertyKey’ and ‘propertyKey’ are incompatible. Type ‘string | symbol’ is not assignable to type ‘string’. Type ‘symbol’ is not assignable to type ‘string’. <ArkTSCheck>

如果propertyKey的类型改为 string | symbol,又会出现’Symbol()’ API is not supported (arkts-no-symbol) <ArkTSCheck> 请问这个怎么解决,


更多关于HarmonyOS鸿蒙Next中如何创建MethodDecorator的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

参考代码

function Validate<T>(errorMsg: T):MethodDecorator {
  return (target: Object, propertyKey: string|Symbol, descriptor: PropertyDescriptor) => {
    console.log('success')
    const originalMethod:Function = descriptor.value;
    descriptor.value = (arg:string):Error|string => {
      console.log('这里写检验逻辑')
      return originalMethod(arguments)
   };
  };
}
@Validate("Name must be a non-empty string.")
load(){
  return 'd'
}

你代码中 propertyKey 的类型应该是 string | Symbol

更多关于HarmonyOS鸿蒙Next中如何创建MethodDecorator的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中创建MethodDecorator,可以通过TypeScript的装饰器机制实现。MethodDecorator是一种特殊的装饰器,用于修饰类中的方法。以下是创建MethodDecorator的基本步骤:

  1. 定义MethodDecorator函数:首先,定义一个函数,该函数接受三个参数:target(类的原型或构造函数)、propertyKey(方法名称)和descriptor(属性描述符)。
function MyMethodDecorator(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
    // 在这里添加自定义逻辑
}
  1. 使用MethodDecorator:在类的方法上使用@MyMethodDecorator来应用装饰器。
class MyClass {
    @MyMethodDecorator
    myMethod() {
        console.log('Original method called');
    }
}
  1. 修改方法行为:在MethodDecorator中,可以通过修改descriptor.value来改变方法的行为。
function MyMethodDecorator(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
    const originalMethod = descriptor.value;
    descriptor.value = function(...args: any[]) {
        console.log('MethodDecorator called before original method');
        const result = originalMethod.apply(this, args);
        console.log('MethodDecorator called after original method');
        return result;
    };
}
  1. 应用装饰器:当调用myMethod时,装饰器中的逻辑会在方法执行前后被触发。
const instance = new MyClass();
instance.myMethod();

在HarmonyOS鸿蒙Next中创建MethodDecorator,可以通过以下步骤实现:

  1. 定义装饰器函数:创建一个函数,该函数接受targetpropertyKeydescriptor三个参数。

  2. 修改方法行为:在装饰器函数中,使用descriptor.value访问原始方法,并通过descriptor.value重新定义方法行为。

  3. 返回描述符:最后,返回修改后的descriptor

function MyMethodDecorator(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
    const originalMethod = descriptor.value;
    descriptor.value = function (...args: any[]) {
        console.log(`Calling method: ${propertyKey}`);
        return originalMethod.apply(this, args);
    };
    return descriptor;
}
  1. 应用装饰器:在类方法上使用@MyMethodDecorator进行装饰。
class MyClass {
    @MyMethodDecorator
    myMethod() {
        console.log('Executing myMethod');
    }
}

这样,MyMethodDecorator会在myMethod被调用时打印日志。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!