angular 4 中的 $location.search() 是什么

what is the $location.search() in angular 4

如何使用angular中的$location.search 4

var queryParams = $location.search(); -- in angular

 setDefaultLanguage() {
    var queryParams = $location.search();
    if (queryParams.locale) {
        var locale = queryParams.locale;
        var selectedLanguageArray = languageOptions.filter(function (lang:any) {
            return String(lang.id).toLocaleLowerCase() === String(locale).toLocaleLowerCase();
        });
    }

您可以使用激活的路线:

export class AppComponent {
     constructor(route: ActivatedRoute) {
         route.queryParams.pipe(
             filter(q => !!q.locale),
             map(q => q.locale as string),
             distinctUntilChanged()
         ).subscribe(locale => console.log(locale));
    }
 }

或者,如果您只想拥有它一次,您可以从快照中获取它:

    if (route.snapshot.queryParams.locale) {
        const locale = route.snapshot.queryParams.locale;
        const selectedLanguageArray = languageOptions.filter(function (lang: any) {
            return String(lang.id).toLocaleLowerCase() === String(locale).toLocaleLowerCase();
        });
    }