在组件中使用地图API时,我们需要在引入地图API的JavaScript代码之前添加以下代码:
declare const google: any;
const apiKey = 'YOUR_API_KEY_HERE';
// 添加这段代码
google.maps.streetViewService = new google.maps.StreetViewService();
// 添加这段代码
@Component({
selector: 'app-example-component',
templateUrl: './example-component.component.html',
styleUrls: ['./example-component.component.css']
})
export class ExampleComponent {
constructor() {
// 需要在构造函数里面初始化以下代码
google.maps.streetViewService = new google.maps.StreetViewService();
// 需要在构造函数里面初始化以上代码
}
}
这个方法可以使应用程序在使用谷歌地图API时避免CORS问题,现在您应该可以成功运行Street View了。