简体   繁体   中英

How to access variable from one component into another component in angular?

我现在有两个组件users_profile_component和userslist组件,我想访问使用@viewchild @Input尝试过的userslist组件中的user_profile_component中的is_added变量,但每次它给我带来无法定义错误的错误时,

Angular Services is exactly what you need. Tons of tutorials on the web, here's one: https://angular.io/tutorial/toh-pt4

The first question in this problem is know where is that variable, in the parent? in the child?.

If you want to hace access from the child component to a parent's property, you must use bindings

class Parent {
   property = 1;
}

//
// Parent HMTL
<child [prop]="property">

class Child {
@Input() prop;
  ngOnInit() {
    console.log (this.prop); // 1
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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