简体   繁体   English

无法将数据传递和访问数据到Angular中的无状态组件

[英]Trouble passing and accessing data to stateless component in Angular

I'm trying something very simple but having alot of trouble. 我正在尝试一些非常简单但有很多麻烦的东西。 I have a container component and child component: 我有一个容器组件和子组件:

-- street.dashboard.component
---- street.search.component

I'm doing a service call and setting data in my dashboard component and then passing it through using 我正在进行服务呼叫并在仪表板组件中设置数据,然后通过使用

<street-search locations="streets"></search> 

however, in my child component, I can not get access to that data within my class? 但是,在子组件中,我无法访问班级中的该数据? I can render it in the template fine and see that it's getting passed through, but cannot access it in my class. 我可以在模板中很好地呈现它,并看到它已经通过,但是无法在我的课程中访问它。

Any ideas? 有任何想法吗?

street-dashbaord.component 街道dashbaord.component

@Component({
    selector: 'street-dashboard',
    styleUrls: ['street-dashboard.component.scss'],
    template: `
       <street-search locations="streets"></street-search>
    `

export class StreetDashboardComponent implements OnInit {
    streets: any;
    constructor(private streetService: StreetDashboardService ) {}
    ngOnInit() {
        console.log("ngOnInit");
        this.streetService
        .getStreets()
        .subscribe((data: Street[]) => this.streets = data);
    }
}

street-search-component 街道搜索组件

@Component({
    selector: 'street-search',
    template: `
        {{ locations.length }} <!-- This renders fine -->
        <md-input-container> 
            <input type="text" mdInput placeholder="Enter your street..."
            [formControl]="myControl" [mdAutocomplete]="auto">  
        </md-input-container>

        <md-autocomplete #auto="mdAutocomplete" showPanel="false">
            <md-option *ngFor="let option of filteredOptions | async" 
            [value]="option" (onSelectionChange)="selected($event, option)">
                {{ option }}
            </md-option>
        </md-autocomplete>
    `
});

export class StreetSearchComponent {
    @Input()
    locations: any;

    constructor () {
       console.log("Locations: ", this.locations); // Undefined ??
    }
}

Despite seeing the locations.length in the browser, I do also get a console error of 尽管在浏览器中看到了locations.length,但我也收到了控制台错误

ERROR TypeError: Cannot read property 'length' of undefined

I've also tried using ngOnInit in the child component but no joy. 我也尝试在子组件中使用ngOnInit,但没有任何乐趣。 Thanks 谢谢

@Input is first available in OnChanges (constructor is run first of all, so not available there), so usually it's available then in OnInit , but since the retrieval of data is async, I'm guessing it will still be undefined when OnInit is run in child. @Input首先在OnChanges可用(首先运行构造函数,因此在那里不可用),因此通常在OnInit才可用,但是由于数据的检索是异步的,所以我猜测当OnInit它仍然是未定义的在孩子中跑。

You can watch for the changes in @Input variable in OnChanges , which is fired when changes happen to the Input variable, so try: 您可以在OnChanges@Input变量中的更改,当Input变量发生更改时会触发该更改,因此请尝试:

ngOnChanges() {
  if(!locations.length) {
    console.log('not yet!')
  } else {
    console.log(locations)
  }
}

And as Aluan kindly pointed out, you have also forgotten [ ] around locations . 正如Aluan所指出的那样,您也忘记了locations附近的[ ] With the brackets you are actually binding your variable streets , so it should be: 使用括号,您实际上是在绑定可变 streets ,因此应该是:

<street-search [locations]="streets"></search>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM