问题可能是由于Web服务器上的基本路径不同于本地开发服务器而引起的。可以将基本路径注入到Angular组件中,以便在调用windows.open时使用。示例代码如下:
在组件中注入基本路径:
import { Component } from '@angular/core'; import { DOCUMENT } from '@angular/common';
@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent {
constructor(@Inject(DOCUMENT) private document: any) {}
basePath = '';
ngOnInit() { this.basePath = this.document.location.href; } }
在调用windows.open时使用basePath:
openNewTab() {
window.open(${this.basePath}/new-page
);
}