gpt4 book ai didi

dart - ngOnInit() 未在 angular2 的组件 Dart 中调用

转载 作者:行者123 更新时间:2023-12-03 02:57:12 24 4
gpt4 key购买 nike

我有一个调用这个函数的 app_component.dart

class OtherComponent {
// ChromeCast receveiver event handler
onLoad() {
this.router.navigate(["/Video", { 'pageData': pageData}]);
}
}

加载另一个名为 video_component.dart 的 dart 组件。
import 'package:angular2/angular2.dart';
import 'package:angular2/common.dart';
import 'package:angular2/router.dart';
import 'dart:js';
import 'dart:html';


@Component(selector: 'video-container', templateUrl: '../../views/video_component.html', directives: const [NgFor])
class VideoComponent implements OnInit {

@Input()
ContentService contentService;

LoggerService log;

String progress;

RouteParams routeParams;

var pageData;

VideoComponent(@Inject(ContentService) this.contentService, @Inject(LoggerService) this.log, this.routeParams) {
log.info('constructor called'); // <== constructor is called
}

@override
ngOnInit() {
log.info('ngOnInit:Load video page with content Id');
//not called
}
}

为什么不 ngOnInit()不被叫?

最佳答案

尝试

NgZone zone;
ParentComponent(this.zone);

onLoad(event) {
zone.run(() => this.router.navigate(...)
}

我假设 ParentComponent 是包含事件处理程序 onLoad(event) ... 的组件,它是构造函数,用于注入(inject) NgZone,就像您在问题代码中对 ContentService 和 LoggerService 所做的那样。将 ParentComponent 更改为您用于此组件的类的名称。
onLoad()来自 ChromeCast 的似乎没有从 Angular 修补,因此不在 Angulars 区域中运行。现在,当从 Angulars 区域之外的代码调用事件处理程序时,某些事情不再起作用(例如更改检测)。

zone.run(...)我们明确地将上下文带回 Angulars 区域。

关于dart - ngOnInit() 未在 angular2 的组件 Dart 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35698570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com