gpt4 book ai didi

angular - Angular 文档中描述的有关 Angular 组件装饰器行为的歧义

转载 作者:搜寻专家 更新时间:2023-10-30 21:34:21 30 4
gpt4 key购买 nike

我是 Angular 的新手,目前我已经看过 angular.io 网站提供的一些示例。但是在component decorator在文档中的解释,它指出

Angular components are a subset of directives, always associated with a template. Unlike other directives, only one component can be instantiated per an element in a template.

模板中的每个元素只能实例化一个组件的实际含义。

谁能给我解释一下。

最佳答案

这实际上意味着在 Angular 生态系统中,模板中的每个元素(选择器)只会实例化一个组件。检查以下示例。

//app.component.ts
import { Component } from '@angular/core';

@Component({
selector: 'app-root',
template: `
<div style="text-align:center">
Welcome to {{ title }}!
</div> `,
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Test App';
}

尝试在 index.html 中使用两个选择器标签

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Angular App</title>
<base href="/">
</head>
<body>
<app-root></app-root>
<app-root></app-root>
</body>
</html>

因此,您将看到只有一个 Welcome to the Test App 在浏览器中呈现。每个 html 元素只能有一个组件

关于angular - Angular 文档中描述的有关 Angular 组件装饰器行为的歧义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54058589/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com