我会在两个产品之间导航,localhost:4200/products/444
到localhost:4200/products/555
。
当我在它们之间导航时,网址会发生变化,但DOM没有发生任何变化,我仍然停留在444
产品,网址为555
。
我正在使用Angular 4
答案 0 :(得分:1)
这是因为Angular重用了该组件。
您必须订阅路线参数以监听更改并相应地更新视图。
TASK [deploy_aker : Copy SSH key L2 users to host] ****************************************************************************************************************************************************************
fatal: [test.florius.com]: FAILED! => {"changed": false, "checksum": "cfcc6cdae818178456c4eca8e89ebce0c14ec91b", "msg": "Destination directory /home/[u'test1', u'test2', u'test3', u'test4']/.ssh does not exist"}
答案 1 :(得分:0)
让您的组件看起来像这样:
您必须从constructor
调用您的函数。
您需要获取ID并从ngOnInit
调用该函数。
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-comp',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
constructor(private route: ActivatedRoute) {
this.myFunction(this.myID); // Remove this line from constructor
}
ngOnInit() {
this.route.params.subscribe(params => {
this.myID = params['id'];
this.myFunction(this.myID); // Add your function call in ngOnInit
});
}
}
要了解constructor
和ngOnInit
之间的区别,请click here