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
);
});
}
Вы можете проверить воспроизведение проблемы в StackBlitz здесь. а>.
Изменить
I mentioned Angular because I want to learn if Angular has an internal method/property to find mapping of class properties metadata which allows me to access static properties of the class in router resolver method. Angular knows the route.component.name. So I thought it may have an internal place to find this kind of metadata.
// 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();
AppSample1Component.prototype.propertyWithDefaultValue
? typescriptlang.org/play/ - person yurzui   schedule 13.11.2017