gpt4 book ai didi

javascript - Google 自动完成器位置在 Angular 2 的子组件中不起作用

转载 作者:数据小太阳 更新时间:2023-10-29 05:32:28 28 4
gpt4 key购买 nike

我正在为 Google places autocompletor 使用 googleplace 指令.如链接所示,当我在 AppComponent 中使用此指令时它会起作用,但当我在子组件中使用它时它不起作用。

app.routes.ts

enter image description here

import { provideRouter, RouterConfig }  from '@angular/router';

import { BaseComponent } from './components/base/base.component';
import { DashboardComponent } from './components/dashboard/dashboard.component';


const routes: RouterConfig=
[
{path:"",redirectTo:"/admin",pathMatch:'full'},

{path:"admin",component:BaseComponent,
children:[
{ path: '', component: BaseComponent},
{ path: 'dashboard', component: DashboardComponent},
]
}

];


export const appRouterProviders = [
provideRouter(routes)
];

ma​​in.ts enter image description here

import {bootstrap}    from '@angular/platform-browser-dynamic';
import {AppComponent} from './app.component';
import {appRouterProviders} from './app.routes';


bootstrap(AppComponent,[appRouterProviders]);

app.component.ts enter image description here

import {Component} from '@angular/core';
import {ROUTER_DIRECTIVES} from '@angular/router';


@Component({
selector : 'my-app',
template: `
<router-outlet></router-outlet>
` ,
directives:[ROUTER_DIRECTIVES]
})
export class AppComponent {

}

base.component.ts enter image description here

import {Component,OnInit} from '@angular/core';
import { provideRouter, RouterConfig,ROUTER_DIRECTIVES,Router } from '@angular/router';



@Component({
selector: 'app-base',
templateUrl:"../app/components/base/base.html",
directives:[ROUTER_DIRECTIVES],
precompile:[]

})



export class BaseComponent implements OnInit{

constructor(private _router:Router){}

ngOnInit():any{

this._router.navigate(["admin/dashboard"]);
}
}

base.html<router-outlet></router-outlet>有内容

dashboard.component.ts enter image description here

import {Component,OnInit} from '@angular/core';

import { provideRouter, RouterConfig,ROUTER_DIRECTIVES,Router } from '@angular/router';
import {GoogleplaceDirective} from './../../../directives/googleplace.directive';



@Component({
selector: 'dashboard',
template:`
<input type="text" [(ngModel)] = "address" (setAddress) = "getAddress($event)" googleplace/>
`,
directives:[ROUTER_DIRECTIVES,GoogleplaceDirective]
})

export class DashboardComponent implements OnInit{

constructor(private _router:Router){}

ngOnInit():any{

// this._router.navigate(["dashboard/business"]);
}

public address : Object;
getAddress(place:Object) {
this.address = place['formatted_address'];
var location = place['geometry']['location'];
var lat = location.lat();
var lng = location.lng();
console.log("Address Object", place);
}
}

googleplace.directive

import {Directive, ElementRef, EventEmitter, Output} from '@angular/core';
import {NgModel} from '@angular/common';

declare var google:any;

@Directive({
selector: '[googleplace]',
providers: [NgModel],
host: {
'(input)' : 'onInputChange()'
}
})
export class GoogleplaceDirective {
@Output() setAddress: EventEmitter<any> = new EventEmitter();
modelValue:any;
autocomplete:any;
private _el:HTMLElement;


constructor(el: ElementRef,private model:NgModel) {
this._el = el.nativeElement;
this.modelValue = this.model;
var input = this._el;
this.autocomplete = new google.maps.places.Autocomplete(input, {});
google.maps.event.addListener(this.autocomplete, 'place_changed', ()=> {
var place = this.autocomplete.getPlace();
this.invokeEvent(place);
});
}

invokeEvent(place:Object) {
this.setAddress.emit(place);
}


onInputChange() {
}
}

index.html enter image description here

输出: enter image description here

更新:

Found that, it works perfectly when there is one router-outlet tag in the project, but fails to work when we have nested router-outlet as above example has nested router-outlet

Github link here

组件的子组件的指令代码是否有任何问题?请告诉我如何解决此问题。

最佳答案

问题是 https://maps.googleapis.com/maps/api/place/js/AutocompletionService.GetPredictions需要一个 api key ,当你在路由器子中使用它时。

index.html

<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY&libraries=places&sensor=false"></script>

将您的 Google API key 替换为 API_KEY。

我无法解释子组件(不需要 api key )和路由器子组件(需要 api key )之间的行为差​​异。

根据 Google Map Api 文档,需要 API key :

https://developers.google.com/maps/documentation/javascript/places-autocomplete

关于javascript - Google 自动完成器位置在 Angular 2 的子组件中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38878023/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com