class AppComponent {
public name: string;
constructor(name) {
console.log('Constructor initialization');
this.name = name;
}
}
let appCmp = new AppComponent('AppCmp'); // 这时候构造函数将被调用。
console.log(appCmp.name);
var AppComponent = (function () {
function AppComponent(name) {
console.log('Constructor initialization');
this.name = name;
}
return AppComponent; // 这里直接返回一个实例
}());
var appCmp = new AppComponent('AppCmp');
console.log(appCmp.name);
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<h1>Welcome to Angular World</h1>
`,
})
export class AppComponent implements OnInit {
constructor() {
console.log('Constructor initialization');
}
ngOnInit() {
console.log('ngOnInit hook has been called');
}
}
// parent.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'exe-parent',
template: `
<h1>Welcome to Angular World</h1>
<p>Hello {{name}}</p>
<exe-child [pname]="name"></exe-child> <!-- 绑定到子组件的属性 -->
`,
})
export class ParentComponent {
name: string;
constructor() {
this.name = 'God eyes';
}
}
// child.component.ts
import { Component, Input, OnInit } from '@angular/core';
@Component({
selector: 'exe-child',
template: `
<p>父组件的名称:{{pname}} </p>
`
})
export class ChildComponent implements OnInit {
@Input()
pname: string; // 父组件的输入属性
constructor() {
console.log('ChildComponent constructor', this.pname); // this.name=undefined
}
ngOnInit() {
console.log('ChildComponent ngOnInit', this.pname); // this.name=God eyes
}
}
import { Component, ElementRef, OnInit } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<h1>Welcome to Angular World</h1>
<p>Hello {{name}}</p>
`,
})
export class AppComponent implements OnInt {
name: string = '';
constructor(public elementRef: ElementRef) { // 使用构造注入的方式注入依赖对象
this.name = 'WXY'; // 执行初始化操作
}
ngOnInit() {
this.gotId = this.activatedRoute.params.subscribe(params => this.articleId = params['id']);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有