如何将以下内容重写为异步函数?它继续将视图上的referralUrl返回为undefined:
调节器
createReferralUrls(){ this.referralUrl = this.referralService ….
你需要在这里实现Observable调用,
import { Subscription } from "rxjs"; ... referralSubscription: Subscription; ... createReferralUrls() { this.referralSubscription = this.referralService.generateReferralUrl(this.userData.referralId) .subscribe( (res)=>{ this.campaignMessage = 'Sign up here: ' + res + '.'; this.facebookShareUrl = 'http://www.facebook.com/sharer/sharer.php?u=' + res; this.whatsappShareUrl = 'https://wa.me/?text=' + this.campaignMessage; this.twitterShareUrl = 'https://twitter.com/intent/tweet?text=' + this.campaignMessage; this.emailShareUrl = 'mailto:?subject=Welcome to our site!&body=' + this.campaignMessage; } ); }
请同时添加您的服务,以便我们也可以编辑您的服务返回类型。
的 UPDATE2: 强> 如下更改您的服务,
generateReferralUrl(referralId) { if (location.host === 'localhost:4200') { return Observable.create((observer) => { observer.next('localhost:4200/invite/' + referralId) }); } else if (location.host === 'myapp.herokuapp.com') { return Observable.create((observer) => { observer.next('myapp.herokuapp.com/invite/' + referralId) }); } else if (location.host === 'myapp.com') { return Observable.create((observer) => { observer.next('myapp.com/invite/' + referralId) }); } }
请改变你的输入 <input type="text" class="form-control" placeholder="" [value]="referralUrl" readonly>
<input type="text" class="form-control" placeholder="" [value]="referralUrl" readonly>