【问题标题】:Passing a variable between two components without inheritance angular 2在没有继承角度2的两个组件之间传递变量
【发布时间】:2018-03-21 05:22:53
【问题描述】:

我正在尝试将单个变量从一个组件传递到另一个组件。它是一个数组,声明如下。

@Component({
  selector: 'component1',
  templateUrl: './component1.component.html',
  styleUrls: ['./component1.css'],
})
export class Component1 implements OnInit{
  ......
  columnVars = Object.keys(this.settings.columns);
  ......
}

所以我设置了我的第二个组件来扩展我的第一个组件并像这样引用变量,它按预期工作。

<table>
  <thead>
    <tr>
      <th>Column</th>
      <th>Description</th>
    </tr>
  </thead>
    <tbody *ngFor="let col of columnVars">
      <tr>
        <td>{{settings.columns[col]['title']}}</td>
        <td>{{settings.columns[col]['description']}}</td>
      </tr>
    </tbody>
</table>

第二个组件看起来像这样,这就是问题所在。

@Component({
  selector: 'component2',
  templateUrl: './component2.html',
  styleUrls: ['./component2.css']
})

export class Component2 extends Component1 implements OnInit {

  buttonObjList = {
    headendsButton: {
      title: 'Back to headends',
      route: '/headends',
    }
  };
}

我得到了错误

ERROR in /home/grady/honeybadger-mat/src/app/column-info/column-info.component.ts (9,14): Class 'ColumnInfoComponent' incorrectly extends base class 'IngestViewComponent'.

  Types of property 'buttonObjList' are incompatible.
    Type '{ headendsButton: { title: string; route: string; }; }' is not assignable to type '{ columnInfo: { title: string; route: string; }; }'.
      Property 'columnInfo' is missing in type '{ headendsButton: { title: string; route: string; }; }'.

这是有道理的。所以现在我的问题是有没有办法在没有继承的情况下将变量传递给另一个组件,或者有没有办法覆盖特定的继承变量,如 buttonObjList?先感谢您。

编辑:澄清一下,我不需要在我的第一个组件中显示第二个组件。所以在这种情况下,@input 指令似乎对我没有好处。如果我错了纠正我。

【问题讨论】:

  • 不清楚您要完成什么。 2个组件有什么关系?在 2 个组件之间传递信息高度依赖于它们的关联方式。您的问题并不清楚这种背景。
  • 它们并不真正相关。他们只是不同的看法。一个组件显示一个表格。另一个组件解释了表格中每一列的含义。
  • 有点困惑,所以如果一个组件解释了由另一个组件表示的表格中每一列的含义,它们应该以某种方式相关,对吧?我的意思是这个列组件是否出现在表格组件的模板定义中?
  • 列组件不显示在表格组件中。这是一个单独的视图。我可以使用什么样的关系不是子组件?我正在考虑使列组件成为一种模式来简化这一点,但我仍然想知道未来的答案。

标签: angular inheritance


【解决方案1】:
you can Achieve following way.

First Method:-(Global Variable Set and Get Example)

Step 1:
Declare a Global Variable in app.component.ts

export class AppComponent  {
    public static dataNavigate;
}
Step2:
import a App Component in Your Value Set Component(first.component.ts)

import {AppComponent} from '../app.component.ts';

AppComponent.columnVars = Object.keys(this.settings.columns);
//Your Value Set here

Now you can access every component in your app.
second.component.ts
import {AppComponent} from './app.component';

constructor(){
  console.log(AppComponent.columnVars);
}



Second Method:-(Common Service)

Step 1:
      Create a Service
dataService.ts
---------
import { Injectable } from '@angular/core';

@Injectable()
export class DataService {
constructor(){

}
public columnVars:any;
}

Step 2:
Import app.module.ts file

import {DataService} from './data.service';

providers:[DataService]

Step 3:
Set the Value to the Common Service Variable

first.component.ts
import {DataService} from './data.service';

constructor(dataService:DataService){
  this.dataService.columnVars="whatever maybe json or object etc..";
}

Step 4:
Access that you can do same way step3
second.component.ts
import {DataService} from './data.service';

constructor(dataService:DataService){
  console.log(this.dataService.columnVars);
}

其他方式也是可能的,您可以将变量设置为某个会话或本地存储并使用它 进一步参观 Local storage in Angular 2

我相信它很有用。

【讨论】:

  • 我无法让全局变量方法工作。可能是最好的,因为这似乎不受欢迎。不过,这项服务工作得很好。谢谢。
  • 很高兴为您提供帮助。谢谢。
猜你喜欢
  • 2018-03-13
  • 2019-09-25
  • 2023-01-19
  • 2013-10-14
  • 1970-01-01
  • 1970-01-01
  • 2012-02-21
  • 2020-09-12
  • 2018-02-22
相关资源
最近更新 更多