export class AppSample1Component {
public propertyWithDefaultValue = 'Lorem ipsum dolor sit amet!';
}
是否可以在 Angular 中访问组件的类原型?
例如(这个没有返回值):
console.log(AppSample1Component.prototype.propertyWithDefaultValue);
如果可能的话,如何在路由解析器函数中加入类的原型?
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> | Promise<any> | any {
const componentType = typeof route.component === 'string' ? route.component : route.component.name;
console.log('Class name of the component: ', componentType);
console.log('>>> I want to find the prototype of component class from ActivatedRouteSnapshot and get the value of "propertyWithDefaultValue" property of that class prototype in this section.');
return new Promise((resolve, reject) => {
Promise.all([
this.getData()
]).then(
() => {
// Init component(s) after prefetch data, etc...
resolve();
},
reject
);
});
}
编辑
我提到 Angular 是因为我想了解 Angular 是否有一个内部方法/属性来查找类属性元数据的映射,它允许我在路由器解析器方法中访问类的静态属性。Angular 知道 route.component.name。所以我认为它可能有一个内部位置可以找到这种元数据。
// Here's an example which shows a minimal use of OOP in my recent javascript projects
const myLib = {
apply: function (obj, config) {
if (obj && config && typeof config === 'object')
for (var p in config)
obj[p] = config[p];
return obj;
},
extend: function (sc, bc) {
var Fn = function () {};
Fn.prototype = bc.prototype;
var scpb = sc.prototype;
var scp = sc.prototype = new Fn();
scp.constructor = sc;
if (bc.prototype.constructor === Object.prototype.constructor)
bc.prototype.constructor = bc;
myLib.apply(scp, scpb);
sc.superclass = bc.prototype;
}
};
myLib.textField = function (config) {
};
myLib.textField.prototype = {
type: 'text'
};
myLib.numberField = function (config) {
myLib.numberField.superclass.constructor.call(this, config);
};
myLib.numberField.prototype = {
type: 'number',
min: 0,
max: 9999
};
myLib.extend(myLib.numberField, myLib.textField);
const obj1 = new myLib.textField();
const obj2 = new myLib.numberField();
console.group('My old JS way');
console.log(obj1.type, obj2.type);
console.log(myLib.textField.prototype.max, myLib.numberField.prototype.max);
console.groupEnd();
// And here is a demonstration of typescript generated javascript.
// Compiled typescript code generates javascript class as something like below.
// Typescript places the properties to the beginning of the constructor, not to prototype.
// This is why it's not possible to access class prototype directly.
myLib.numberField2 = function () {
this.type = 'number';
this.min = 0;
this.max = 9999;
//constructor block begin
//constructor block end
};
const obj3 = new myLib.numberField2();
console.group('Typescript compiled output demonstration');
console.log(obj3.max); // prints 9999
console.log(myLib.numberField2.prototype.max);
// this prints "undefined" because max property doesn't exist before object creation
console.groupEnd();