简体   繁体   English

为什么自定义组件在ionic 2中创建页面之前运行?

[英]why does the custom component run before the page is created in ionic 2?

I would like to implement a custom component in my ionic project and I followed this guide to realize it. 我想在我的离子项目中实现自定义组件,并按照本指南进行实现。

On my page, instantiate a variable to pass to my component 在我的页面上,实例化一个变量以传递给我的组件

export class NewSegmentPage {

name: any;

constructor(public navCtrl: NavController, public navParams: NavParams) {
this.name = "hello";      
}

and in the template 并在模板中

...
<ion-content>
  <slider [name]="name"></slider>
</ion-content>

Now I create my component 现在我创建我的组件

<ion-item>
  <ion-range dualKnobs="true" pin="true" [(ngModel)]="structure"></ion-range>
</ion-item>

and retrieve the data that I pass to the variable by @Input 并通过@Input检索传递给变量的数据

@Input("coordinates") activity;
structure: any = { lower: 33, upper: 60 };

constructor() {
   console.log(this.activity);
}

but this.activity variable is undefined. 但是this.activity变量是不确定的。

In addition, in the google console I saw that the component is created before the page and that's why it fails to pass the data. 另外,在Google控制台中,我看到该组件是在页面之前创建的,这就是为什么它无法传递数据的原因。 But, How does it behave like that? 但是,它的行为如何?

The execution is right but I was wrong to retrieve the this.activity variable in the constructor. 执行是正确的,但是在构造函数中检索this.activity变量是错误的。 Off of my constructor my variable is instantiated. 从我的构造函数中,我的变量被实例化。

ngAfterViewInit() {
   console.log(this.activity);    
}

暂无
暂无

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

相关问题 组件和运行功能的离子打开页面或选项卡 - Ionic open page or tab from component and run function 为什么将<script>附加到动态创建的<iframe>似乎在父页面中运行脚本? - Why does appending a <script> to a dynamically created <iframe> seem to run the script in the parent page? @Input无法从延迟加载的页面在ionic 3自定义组件中工作 - @Input not working in ionic 3 custom component from a lazy loaded page AJAX:跳转到PHP使用xmlhttp.open动态创建的页面的另一部分(onClick()是否在href之前运行?) - AJAX: Jumping to another section in page created dynamically by PHP using xmlhttp.open (does onClick() run before href?) 自定义Ionic幻灯片组件{Slides}导入参考仅更新上次创建的幻灯片组件 - custom Ionic slides component {Slides} import reference only updates last created slide component 为什么我的函数在 componentDidMount 之前运行 - Why does my function run before componentDidMount 为什么在功能组件的返回语句中的控制台之前运行 useEffect 中的警报? - Why alert in useEffect run before console in return statement in a functional component? 为什么打开项目目录后“离子服务”无法运行? - Why “ionic serve” does not run once I open project directory? 在ionic 2中缓存自定义组件的内容 - Cache custom component content in ionic 2 为什么在组件安装之前Axios会继续发送请求? - Why does Axios keep sending requests before component mounts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM