Angular17

Angular17

创建angular17项目

ng new project001 --style=scss --skip-tests --routing=false --ssr=false -s -t
–style=scss 使用scss作为css
–skip-tests 不生成测试代码
–routing=false 不生成路由代码
–ssr=false 不支持ssr(服务端渲染)
-s (–inline-style)移除.scss文件,直接在ts文件中写样式
-t (–inline-template) 移除.html文件,直接在ts文件中写

npm run ngc -p tsconfig.json 编译成js文件输出(dist/out-tsc)

独立组件可以直接导入,无需在模块中声明

1
2
3
4
5
6
7
8
9
10
11
12
@Component({
standAlone: true,
selector: 'demo'
})
export class demo {}

@Component({
standAlone: true,
selector: 'HelloWorld',
template: `<demo />`
})
export class helloWorld {}

组件传值pipe

1
2
3
4
5
6
7
export class demo {
@Input({ transform: trimString }) label = ''
}

function trimString(value: string | undefined) {
return value?.trim() ?? ''
}

插槽

组件 selector: 'custom-item'

1
2
3
4
<div>
<ng-content select="card-title"></ng-content>
<ng-content select="card-body"></ng-content>
</div>

组件使用1

1
2
3
4
<custom-item>
<card-title>插槽title</card-title>
<card-body>插槽body</card-body>
</custom-item>

组件使用2

ngProjectAs 挂在内容到任意html标签

1
2
3
4
<custom-item>
<h2 ngProjectAs="card-title">插槽title</h2>
<card-body>插槽body</card-body>
</custom-item>

use service

serviceDemo

1
2
3
4
5
6
7
8
9
import {Injectable} from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class CalculatorService {
add(x: number, y: number) {
return x + y;
}
}

usage

1
2
3
4
5
6
7
8
9
@Component({
selector: 'app-receipt',
template: `<h1>The total is {{ totalCost }}</h1>`,
})
export class Receipt {
// 在非构造函数中使用
private calculatorService = inject(CalculatorService);
totalCost = this.calculatorService.add(50, 25);
}

ng-content && ngProjectAs

Component template

1
2
3
4
5
<div class="card-shadow">
<ng-content select="card-title"></ng-content>
<div class="card-divider"></div>
<ng-content></ng-content>
</div>

Using the component1

1
2
3
4
<custom-card>
<card-title>Hello</card-title>
<p>Welcome to the example</p>
</custom-card>

Using the component2

1
2
3
4
<custom-card>
<h3 ngProjectAs="card-title">Hello</h3>
<p>Welcome to the example</p>
</custom-card>

本文作者: 孟 虎
本文链接: https://menghu1994.github.io/blog/2024/01/Angular17/index/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!