【发布时间】:2025-12-06 16:05:02
【问题描述】:
我正在使用 @ViewChild 将值传递给子模板函数 - 基本上,父级将值传递给下面的 transition() 函数,将其传递到控制台日志没有问题,但我希望在模板,上面写着“价值是 {{ 价值}}”,但它只是保持空白.. 有什么想法吗?
import {Component, Input, Output, EventEmitter} from '@angular/core';
@Component({
selector: 'reptilehaus-loader',
template: `<div class="loader">THE VALUE IS {{ value }}</div>`,
styles: [`
.loader {
background-color: white;
height: 100px;
width:100px;
position:absolute;
top:0;
right:0;
color: black;
z-index:99999;
}
`]
})
export class TransitionComponent {
value: string;
constructor() {}
transition(value) {
console.log(value);
}
}
父视图
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {ProjectsMainApi} from '../../../services/projects-main';
import 'rxjs/Rx';
import {Observable} from 'rxjs/Observable';
import { ROUTER_DIRECTIVES } from '@angular/router-deprecated';
import {CanDeactivate, RouteTree} from '@angular/router';
import {TransitionComponent} from '../../PageLoader/TransitionComponent';
declare var jQuery: any;
// <a class="project-display-matrice" *ngFor="let projekt of project | async" [routerLink]="['ProjectDetail', { projectlink: projekt.slug }]" >
@Component({
selector: 'projects',
template:
`<div> template here </div>
</a><loader></loader>`,
directives: [ROUTER_DIRECTIVES,TransitionComponent]
})
export class ProjectsListComponent implements CanDeactivate, OnInit {
elementRef: ElementRef;
project: Observable<any>;
@ViewChild(TransitionComponent) transitionComponent: TransitionComponent;
constructor() {
}
ngOnInit() {
}
routerCanDeactivate(currTree?: RouteTree, futureTree?: RouteTree): Promise<boolean> {
console.log('PROJECT LIST');
// console.log('Item: ', currTree);
this.transitionComponent.transition("sdfsfsdf");
return new Promise((resolve, reject) => {
jQuery('.loader').addClass('show');
setTimeout(() => resolve(true), 1000);
});
}
changeRoute() {
alert('test');
}
}
基本上,每当有人单击路由时,routerCanDeactivate 应该会产生延迟并显示加载组件,但我对如何完成此操作非常迷茫,所以我可能完全走错了方向......基本上是当用户单击链接时我希望一个 div 与我的徽标一起在屏幕上转换,并在页面准备就绪时消失。
目前 routercandeactive 只是将一个字符串传递给我的子视图中的函数,也就是使用 this.transitionComponent.transition("sdfsfsdf"); 的加载器;
【问题讨论】:
-
为什么不使用绑定将值从父级传递给子级。我没有看到足够的上下文来确定你试图完成什么。
-
我是 ng2 的新手,所以我还没有进入绑定。我正在尝试做一个预加载器,所以基本上当用户导航到父页面时,我想显示上述组件 2 秒,然后使用超时删除一个类
-
嗯,这很抽象。您能否提供更多代码来显示事物是如何连接的?
-
是的,对不起,我可能解释得不好..我已经更新了更多代码和一些信息
-
我试过了。请参阅下面的答案。
标签: angular angular2-template angular2-directives