简体   繁体   English

在Angular 2中使用ng2共享按钮进行共享

[英]Sharing using ng2 share buttons in Angular 2

I have implented sharing functionality in my angular 2 project using ng2 Share buttons pluggin and it is working properly excpet one thing ie, after the feed is shared and some user clicks on that shared feed he gets redirected to the the link I shared but I want him to land up on my websites login page for authentication purpose? 我使用ng2共享按钮pluggin在我的angular 2项目中使用了共享功能,它可以正常工作,这就是一件事,即在共享源和某些用户单击该共享源后,他被重定向到我共享但我想要的链接他登陆我的网站登录页面进行身份验证?

Here is my HTML code:- 这是我的HTML代码:-

          <div class="flip-container" ontouchstart="this.classList.toggle('hover');">
            <div class="flipper">
              <div class="front">
                  <img  class="like_2" alt="image" (click) = "shareIt(i)" src="assets/img/Fwd Icons/share.png">
              </div>
              <div class="back">
                              <div class="row" >
                      <section class="widget sharing_buttons_widget">
                            <share-buttons  (click) = "closeShareIt(i)" [url]=" myFeed.contentUrl "
                                [count]="false"
                                [totalCount]="true"
                                [defaultStyle]= "true"
                                (popUpClosed) = "true"
                                [google]="googleInner"
                                [pinterest]="false"
                                [linkedIn]="false"
                                [tumblr]="tumblrInner"
                                [reddit]="false"
                                [stumbleUpOn]="false">
                          </share-buttons>                
                      </section>
                  </div>
              </div>
            </div>
          </div>

and in my component file:- 在我的组件文件中:

   hideFeed(myFeed){
      const FeedStar = {
      'FeedUId' : myFeed.uid
      };
       this.feedService.DynamicArticles(FeedStar).subscribe(
       data => {

          });  

          this.iframeURL = this.sanitizer.bypassSecurityTrustResourceUrl(       myFeed.contentUrl);
          this.showIframe = true;        


    }

Okay, first of all, the question is wrong. 好吧,首先,这个问题是错误的。 You need to authenthicate the article. 您需要对文章进行身份验证。 In order to use that you need to use can Activate property of angular2 为了使用您需要使用的can Activateangular2属性

In your routing module: 在您的路由模块中:

import { LoginService } from './login/login.service'
import { article } from './helpers/result/article.component'

const routes: Routes = 
[
{ path: 'article/:id', canActivate:[LoginService], component:article}
];

Explanation: article is the component you want to protect. 说明:article是您要保护的组件。 LoginService the service that will return true if the user is logged in if not false. LoginService服务,如果用户登录为假,则返回true。

Below is the snippet of login service. 以下是登录服务的代码段。

import { ActivatedRouteSnapshot, CanActivate, Router } from '@angular/router';
@Injectable()
export class LoginService implements CanActivate {
canActivate(route: ActivatedRouteSnapshot): boolean {

if(user.loggedIn) 
{
return true;
}
else
{
this._router.navigate(['/login']);
return false
}
}
}

Explanation: the canActivate method is extended from the class canActivate. 说明:canActivate方法是从canActivate类扩展的。 This method where your logic falls. 这种方法属于您的逻辑。 You can check if the user is logged in return true... then user can see the article, if you return false, it will navigate to login. 您可以检查用户是否登录返回true ...,然后用户可以查看该文章,如果返回false,它将导航至登录。 You cant just return false without navigate. 没有导航你不能只是返回false。 because in that case the user will stay on that page. 因为在这种情况下,用户将停留在该页面上。

Ref: https://angular.io/docs/ts/latest/api/router/index/CanActivate-interface.html You may wish to read the above reference to understand better and may be implement in a different logical manner. 参考: https : //angular.io/docs/ts/latest/api/router/index/CanActivate-interface.html您可能希望阅读上述参考文献以更好地理解并且可以以不同的逻辑方式实施。 This is static approach i have been using for my application. 我一直在为应用程序使用这种静态方法。 There might be more. 可能还有更多。

PS: Please let me know if i am doing in a wrong manner. PS:请告诉我我做错了什么。 Comments are welcomed. 欢迎发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM