gpt4 book ai didi

特定组件中的 Angular 2 声明

转载 作者:太空狗 更新时间:2023-10-29 17:48:43 28 4
gpt4 key购买 nike

上次我使用 Angular2 时,指令仍然存在,但现在 app.modules.ts 中有所谓的声明。问题是,如果我打算只在特定组件而不是主要组件中使用指令,我该怎么做?

客户.component.ts

import { Component } from '@angular/core';

@Component({
selector: 'app-customers',
templateUrl: 'app/customer/customers.component.html'
})

export class CustomersComponent {
customers = [
{ id: 1, name: 'Mix'},
{ id: 2, name: 'Ian'},
{ id: 3, name: 'Aniel'},
{ id: 4, name: 'Jess'},
{ id: 5, name: 'Jusi'},
];
}

客户.component.ts

import { Component, Input } from '@angular/core';

@Component({
selector: 'app-customer',
templateUrl: 'app/customer/customer.component.html'
})

export class CustomerComponent {
@Input() customer: {id: number, name: string};

myColor = 'gray';
}

应用程序模块.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';
import { CustomerComponent } from './customer/customer.component';
import { CustomersComponent } from './customer/customers.component';

@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ AppComponent, CustomerComponent, CustomersComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }

我在想的是,当我将一个组件放在一个声明中时,每个组件都可以访问它,而不像以前我只能在一个特定的组件中声明它。如果我在这里错了,请纠正我,如果在声明指令方面有更好的选择,请提出建议。

最佳答案

您可以将组件声明放入 Feature Module 中.
Feature Module应该导入到应用程序模块中。

现在由您决定是否认为您的组件是一个功能并且是否值得一个单独的模块。
我知道模块的所有这些事情并不像指令那样简单明了,但是 Angular2 中有一些设计问题无法通过其他方式解决。

文档:https://angular.io/guide/feature-modules

关于特定组件中的 Angular 2 声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39629071/

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