Created
December 28, 2020 07:47
-
-
Save coder054/f0ffa63f178b5334268261465e80450c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type TemplateType = { | |
template: string; | |
id: string; | |
}; | |
function withTemplate(templateObj: TemplateType) { | |
return function (constructor: any) { | |
let el = document.getElementById(templateObj.id); | |
let p = new constructor(); | |
if (el) { | |
let content = templateObj.template.replace(/{{\w+}}/g, function (a1) { | |
let property = a1.replace("{{", "").replace("}}", ""); | |
return p[property]; | |
}); | |
el.innerHTML = content; | |
} | |
}; | |
} | |
@withTemplate({ template: `<h1>Name: {{name}}, Age: {{age}}</h1>`, id: "app" }) | |
class Person { | |
name = "tuan anh"; | |
age = 28; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment