简体   繁体   English

如何以角度从一个ts文件获取变量到所有其他ts文件

[英]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.谢谢你。

behaviorsubject 行为主体

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.

相关问题 在 Angular 上的另一个组件(between.ts 文件)中使用一个变量 - Use a variable from one component in another (between .ts files) on Angular angular 如何从一个 ts 文件中使用两个 html 文件 - angular how to use two html file from one ts file 从 json 文件中获取数据到 Angular ts - Get data from a json file into Angular ts Angular:如何从.ts文件渲染HTML? - Angular: How to render HTML from a .ts file? 如何从角度的ts文件打开模型? - How to open model from ts file of angular? 获取Angular 2 .ts文件而不是.d.ts文件 - Get Angular 2 .ts files instead of .d.ts files 从.ts文件中导出多个变量,然后以角度4导入到另一个文件中 - Export multiple variable from .ts file and import into another file in angular 4 来自其他components.ts文件中的app.component.ts的Angular ngx编辑器外包配置 - Angular ngx-editor outsource config from app.component.ts in other components.ts file 如何使用ts2代码从angular2 ui路由器将参数从一种状态传递到另一种状态? - how to pass parameters from one state to other using angular2 ui router form ts code? 有没有办法从angular 6通用的server.ts文件中的interceptor.service.ts访问变量? - Is there a way to access a variable from interceptor.service.ts in server.ts file in angular 6 universal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM