我试图更改按钮图像src并将用户路由到另一个页面。这是我尝试过的。Angular 2单击事件:更改图像,然后更改路由
home.component.html
<button class="hexagon"
(click)="gotoFinishing()">
<img [src]="imgsrc">
</button>
home.component.ts
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
@Component({
templateUrl: 'home.component.html'
})
export class HomeComponent {
constructor(private router: Router) { }
imgsrc="test.png";
gotoFinishing(){
this.imgsrc="test2.png";
let link = ['/finishing'];
this.router.navigate(link);
}
}
它不会改变图像的src,但它确实路由用户到正确的页面。有没有办法做到这一点?
谢谢!
如果图像发生变化,您会如何知道图像是否会变化?如果您要从该组件中取消路由,那么甚至需要更改家庭组件中的映像? :) – Alex
@ AJT_82它不会让我发布图像,否则我会告诉你的目标。我们有两个图像(1)有阴影的六角形和(2)没有阴影的六角形。我们试图在点击时创建一个按下按钮,然后路由到页面。那有意义吗?如果我愿意尝试一种更好的方式,但由于按钮是六角形,这很难。 – JessySue
嗯,它不会工作,因为'goToFinishing'更改图像和路由远离组件,新图像没有时间呈现之前,你是路由:) – Alex