location.search未定义,但location是应用程序运行的位置



我的应用程序中有这段代码。

const queryString = require('query-string');
const parsed = queryString.parse(location.search);
return this.docListingService.getPacks(parsed.policy).then(packs => {
this.setState({ isLoading: false, packs });
return packs;
}).catch((error) => {
this.loggingService.logError('Error returning Docs ' + error);
this.setState({ errorOccured: true});
});

它不起作用,因为location.search就是undefined

我可以看到location就是http://localhost:3000/,它就是这个应用程序运行的地方。如何设置?

试着调查一下:

  • https://developer.mozilla.org/en-US/docs/Web/API/Location/search
  • https://www.w3schools.com/jsref/prop_loc_search.asp

位置搜索是一个函数,它返回url的queryString。您的URL中没有queryString..你的/ull/url?queryStringOverHere=某种

不能在字符串上使用点号

基于此你可以试试这个(如果你使用的是react钩子(,首先定义位置:const location = useLocation();

相关内容

最新更新