有什么方法可以在 Angular 的 HTML 模板中提供的类中拥有私有属性吗?

发布于 2025-01-08 23:48:40 字数 956 浏览 1 评论 0原文

我正在制作一些自定义组件,这些组件将在我的项目的不同部分(可能不止一个项目)中使用,并且我希望有一些属性可以在这些组件的 HTML 模板中呈现,但也不能作为 < code>public 对于可能包含该组件的其他组件。

例如:

我的通用组件

@Component({
    selector: 'my-component',
    templateUrl: './my-component.component.html',
    styleUrls: ['./my-component.component.scss'],
})
export class MyComponent implements OnInit {
    title:string='TITLE'; // public because otherwise the template can't use it
}

在其模板中包含 MyComponent 的其他组件

@Component({
    selector: 'other-component',
    templateUrl: './other-component.component.html',
    styleUrls: ['./other-component.component.scss'],
})
export class OtherComponent implements OnInit {
    @ViewChild(MyComponent) my:MyComponent;
    ...
    fun(){
        this.my.title = "other title"; // this is what I don't want to happen
    }
    ...
}

有没有办法避免 OtherComponent 能够使用 MyComponent.title

I'm making a few custom components that will be used in different parts of my project (possibly more than one project) and I wanted to have a few properties to render in the HTML template of those components, but also not be available as public for other components that might include the said component.

For example:

My generic component

@Component({
    selector: 'my-component',
    templateUrl: './my-component.component.html',
    styleUrls: ['./my-component.component.scss'],
})
export class MyComponent implements OnInit {
    title:string='TITLE'; // public because otherwise the template can't use it
}

Other component that includes MyComponent in its template

@Component({
    selector: 'other-component',
    templateUrl: './other-component.component.html',
    styleUrls: ['./other-component.component.scss'],
})
export class OtherComponent implements OnInit {
    @ViewChild(MyComponent) my:MyComponent;
    ...
    fun(){
        this.my.title = "other title"; // this is what I don't want to happen
    }
    ...
}

Is there a way to avoid OtherComponent to be able to use MyComponent.title?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墟烟 2025-01-15 23:48:40

无法将 Angular 组件上的私有属性公开给组件的 HTML 模板。

您可以通过提供 get 方法而不是 set 方法将该属性公开为只读属性:

private _title:string='TITLE'; 
get title(): {
   return this._title;
}

在第二个组件中,此方法应该抛出错误,因为 title 是只读属性:

this.my.title = "other title"; 

您应该能够如果需要,可以访问该值,如下所示:

console.log(this.my.title);

但是,将不可设置。

这里需要注意的是,如果您不使用文字,而是使用数组或对象,则即使不设置主属性,也可以修改对象属性。

private _myObject = {}; 
get myObject(): {
   return this._myObject;
}

那么这样就可以正常工作了:

this.my.myObject.title = "other title"; 

It is not possible to expose private properties on an Angular component to the component's HTML Template.

You might be able to expose that property as a read only property by supplying a get method, but not a set method:

private _title:string='TITLE'; 
get title(): {
   return this._title;
}

In your second component, this method should throw an error, because title is a read only property:

this.my.title = "other title"; 

You should be able to access the value if you wanted, like this:

console.log(this.my.title);

But, will not be settable.

The caveat here is that if you're not using a literal, instead an array or object, the object properties can be modified even without setting the main property.

private _myObject = {}; 
get myObject(): {
   return this._myObject;
}

Then this would work fine:

this.my.myObject.title = "other title"; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文