NativeScript提供了一种将自定义属性添加到您选择的组件的方法,如此处所述:https://docs.nativescript.org/core-concepts/properties。 有什么方法可以向所有现有组件添加自定义属性?就像Angular ng指令一样。
我正在将NS与普通JS一起使用。
已编辑:添加了工作代码示例。
app.js
const application = require("tns-core-modules/application");
const Property = require("tns-core-modules/ui/core/properties").Property;
const View = require("tns-core-modules/ui/core/view").View;
const hiddenProperty = new Property(
{
name: "hidden",
defaultValue: false,
affectsLayout: true,
valueChanged: (target, oldValue, newValue) =>
{
console.log("New value is: " + newValue);
},
});
hiddenProperty.register(View);
application.run({ moduleName: "app-root" });
/*
Do not place any code after the application has been started as it will not
be executed on iOS.
*/