如果您想使用路由器,您可以将routerLink
指令添加到您的按钮或链接。您需要事先设置您的路线。
<button routerLink="/link/toPage">Go to page</button>
如果您想以编程方式进行路由,您可以使用路由器的导航功能。
import { Router } from @angular/router
@Component({
selector: 'my-app',
template: '<button (click)="goToPage()">Go to page</button>'
})
export class AppComponent {
constructor(private _router: Router){ }
goToPage(){
this._router.navigate("/link/toPage");
}
}
更多的angular2的router documentation。
-------------- --------------编辑
如果你想用一个按钮去一个链接,你可以通过使用window.location.href
在JavaScript中完成。这也适用于链接到角度位置,如果你在它之外。
<html>
<head>
<script>
function goToPage(){
window.location.href = "http://www.google.com";
}
</script>
</head>
<body>
<button onclick="goToPage()">google</button>
</body>
</html>
这些替代方法将无法正常工作,因为我将html添加到组件的[innerHTML]中,我无法连接到角码。哪些游戏我的第一个问题,关于添加(点击)到我的HTML,它不会工作... – staffang
不,'(点击)'不能工作,因为这将需要角运行。如果你想使用普通html/js中的按钮转到页面,可以使用'window.location.href'来完成。我编辑了我的答案,包括一个简单的例子。 –