以下是代码段:
import { 路由器 来自" @ angular / router&#34 ;; import { HttpClient的 来自" @ angular / common / http&#34 ;; import { 环境 来自" ../../ environment / environment&#34 ;; import { 地点, LocationStrategy, PathLocationStrategy 来自' @ angular / common&#39 ;;
@Injectable()
export class CommonServicesService {
PathLocation: Location;
referralCode: any = localStorage.getItem('referenceCode');
constructor(
location: Location,
) {
this.PathLocation = location;
}
redirectAfterSuccessfulLogin() {
if (localStorage.getItem("redirectUrl")) {
let url = localStorage.getItem("redirectUrl");
localStorage.removeItem("redirectUrl");
this.PathLocation.prepareExternalUrl("'/'+this.referralCode"); //is this the correct way?
console.log(this.PathLocation);
this.router.navigate([url]);
} else {
this.PathLocation.prepareExternalUrl("'/'+this.referralCode");
console.log(this.PathLocation);
this.router.navigate(["/"]);
}
}
}
答案 0 :(得分:0)
你可以这样做:
getBaseUrl = function(code){
return `localhost:3000/${code}`
}
现在您可以将其用作:
getBaseURl(1234)
它返回:“localhost:3000/1234”
现在您可以在此网址后添加更多路径。
或强>
你可以对这两个URL进一步使用相同的功能:
getBaseUrl = function(code){
if(code == 0000) return localhost:3000
else return `localhost:3000/${code}`
}
现在无论何时调用该函数,您都需要传递 0000 以获取非登录条件Base Url和4位数代码以获取登录后基本URL
getBaseURl(1234) // for login one
getBaseURl(0000) // for non-login one