Angular项目集成ngx-google-places-autocomplete时出现NG0301错误:未找到'ngx-places'导出
解决NG0301: Export of name 'ngx-places' not found! 错误
你遇到的这个错误是因为模板里引用的指令导出名称ngx-places和当前使用的ngx-google-places-autocomplete库版本不匹配导致的,下面是具体的解决步骤:
1. 修正模板中的指令导出引用
在你的模态框输入字段中,把#placesRef="ngx-places"改为#placesRef="ngxGooglePlacesAutocomplete"——新版本的库已经变更了指令的导出名称:
<input ngx-google-places-autocomplete [options]='options' #placesRef="ngxGooglePlacesAutocomplete" (onAddressChange)="handleAddressChange($event)"/>
2. 确保组件正确导入指令
检查你的组件文件,确认已经从库中正确导入GooglePlaceDirective:
import { GooglePlaceDirective } from 'ngx-google-places-autocomplete';
3. 定义options配置对象
指令需要必填的配置项,在组件类中添加options的定义(可根据需求调整规则):
public options: any = { types: ['address'], // 限制只返回地址类结果 componentRestrictions: { country: 'US' } // 可选:限制特定国家的地点结果 };
4. 验证库版本(可选)
如果上述步骤未解决问题,可以检查当前安装的库版本:
npm list ngx-google-places-autocomplete
若使用的是较旧版本,建议升级到最新稳定版:
npm install ngx-google-places-autocomplete@latest
完成这些调整后,重新启动Angular项目,应该就能解决这个导出名称未找到的错误了。
内容的提问来源于stack exchange,提问作者Ragnar921
相关产品推荐
相关产品推荐

