[英]how to get variable from one ts file into all other ts files in angular
I have a variable called name
in home.component.ts file, I want to use this variable globally in all the files how can I do that.我在 home.component.ts 文件中有一个名为
name
的变量,我想在所有文件中全局使用这个变量,我该怎么做。
here is my code:这是我的代码:
app.component.html应用程序组件.html
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a>{{ bd_main }}</a></li>
</ol>
</nav>
home.component.ts home.component.ts
export class HomeComponent implements OnInit {
bd_main= "Home";
}
if we see here I am passing the bd_main
value dynamically from home.components.ts which is one folder but I want to update that in app.component.html which is in another folder.如果我们在这里看到我正在从
bd_main
动态传递bd_main
值,这是一个文件夹,但我想在另一个文件夹中的 app.component.html 中更新它。
how can I do that?我怎样才能做到这一点?
here the subject-behaviour comes with the subject-behaviour you can get and use the value of the declared variable across the whole app.在这里,主题行为带有主题行为,您可以获得并在整个应用程序中使用声明变量的值。 here below link is a little example of subject-behaviour hope you get some from the there.
下面的链接是主题行为的一个小例子,希望你能从那里得到一些。 thank you.
谢谢你。
Create a service that will be provided in root or app module.创建将在 root 或 app 模块中提供的服务。 Within this you can have the variables that you want to share between multiple components.
在此,您可以拥有要在多个组件之间共享的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.