seongbin9786 / my-angular

Immitation of Angular framework
0 stars 0 forks source link

Make it able to render - the most simple case (including implementation of `Component`, `Service`, `NgModule`) #1

Open seongbin9786 opened 5 months ago

seongbin9786 commented 5 months ago

Render the output below when the input below presented.

Requirements

Requirements can be changed along with the progress of development of this issue.

Output

Rendered HTML

<body>
    <my-component>
          <div>
              <div>hello world</div>
              <span>for the {{callCounter}} times!</span>
              <button (click)="handleClick()">Click me to raise the number!</button>
         </div>
    </my-component>
    <script src="./dist/main.js"></script>
</body>

Input Source Code

index.html

<body>
    <my-component></my-component>
    <script src="./dist/main.js"></script>
</body>

boostrap call

bootstrapModule(MyModule);

Component

@Component({
    selector: 'my-component',
    template: `
    <div>
        <div>hello world</div>
        <span>for the {{callCounter}} times!</span>
    </div>
    `,
})
class MyComponent {
    callCounter: number = 0;

    constructor(private myService: MyService) {
    }

    handleClick() {
        this.callCounter++;

        console.log(`handle click for ${this.callCounter} times!`);

        this.myService.doSomeService();
    }
}

Service

@Injectable
class MyService {

    doSomeService() {
        console.log('doing service!');
    }
}

NgModule

@NgModule({
    declarations: [ MyComponent ],
    imports: [],
    providers: [ MyService ],
    bootstrap: [ MyComponent ],
})
export class MyModule {}
seongbin9786 commented 4 months ago

Options for implementing this feature:

Option 1. Implement it as similar as how Angular does

Option 2. Implement it as simple as possible