【问题标题】:How to add Social share to a post Ionic wordpress app如何将社交分享添加到帖子 Ionic wordpress 应用程序
【发布时间】:2018-06-30 06:36:56
【问题描述】:

我一直在尝试在我的应用中为帖子插入一个分享按钮,该按钮能够使用安装在 android 手机中的默认应用,但似乎无法通过。

这就是我的 post.ts 文件的样子

import { Component } from '@angular/core';
import { NavParams, NavController, AlertController } from 'ionic-angular';
.
.
import { SocialSharing } from '@ionic-native/social-sharing';


/**
 * Generated class for the PostPage page.
 */


@Component({
  selector: 'page-post',
  templateUrl: 'post.html'
})
export class PostPage {

  post: any;
  user: string; 
  comments: Array<any> = new Array<any>();
  categories: Array<any> = new Array<any>();
  morePagesAvailable: boolean = true;

  constructor(
    public navParams: NavParams,
    public navCtrl: NavController,
    public alertCtrl: AlertController,
    private socialSharing: SocialSharing
  ) {

  }

  ionViewWillEnter(){
    this.morePagesAvailable = true;

    this.post = this.navParams.get('item');

    Observable.forkJoin(
      this.getAuthorData(),
      this.getCategories(),
      this.getComments())
      .subscribe(data => {
        this.user = data[0].name;
        this.categories = data[1];
        this.comments = data[2];
      });
  }

  getAuthorData(){
    return this.wordpressService.getAuthor(this.post.author);
  }

  getCategories(){
    return this.wordpressService.getPostCategories(this.post);
  }

  getComments(){
    return this.wordpressService.getComments(this.post.id);
  }

  loadMoreComments(infiniteScroll) {
    let page = (this.comments.length/10) + 1;
    this.wordpressService.getComments(this.post.id, page)
    .subscribe(data => {
      for(let item of data){
        this.comments.push(item);
      }
      infiniteScroll.complete();
    }, err => {
      console.log(err);
      this.morePagesAvailable = false;
    })
  }

  goToCategoryPosts(categoryId, categoryTitle){
    this.navCtrl.push(HomePage, {
      id: categoryId,
      title: categoryTitle
    })
  }


// Social sharing function is here

 sharePost() {

    this.socialSharing.share("Post Excerpt", "Post Title", "Post Image URL", "Post URL")
    .then(() => {
      console.log("sharePost: Success");
    }).catch(() => {
      console.error("sharePost: failed");
    });

  }

}

问题 如何将帖子标题、网址帖子图片(REST API - JSON)插入this.socialSharing.share("Post Excerpt", "Post Title", "Post Image URL", "Post URL") 让分享按钮看起来更像这样

<button ion-fab class="btn share" mini (click)="sharePost()">&#xF497;</button>

编辑 我已经设法使用

sharePost() {

    this.socialSharing.share(this.post.excerpt.rendered, this.post.title.rendered, this.post.images.large, this.post.link)
    .then(() => {
      console.log("sharePost: Success");
    }).catch(() => {
      console.error("sharePost: failed");
    });

  }

但是,当我喜欢使用 gmail 分享时,会显示 html 特殊字符

e.g title shows: catering &#038; Cleaning Services 
Excerpt shows:   <p>Some text[&hellip;]</p>

我如何摆脱那些 html 字符,只显示一些干净的文本。?

谢谢

【问题讨论】:

    标签: android json wordpress cordova ionic-framework


    【解决方案1】:

    我从我的 wordpress 帖子中删除 html 标记的一种方法是创建一个管道,然后在将摘录呈现到视图之前通过管道传递摘录

    Pipe.ts 就是这样

    import { Pipe, PipeTransform } from '@angular/core';
    
    /**
     * Generated class for the RemovehtmltagsPipe pipe.
     *
     * See https://angular.io/api/core/Pipe for more info on Angular Pipes.
     */
    @Pipe({
      name: 'RemovehtmltagsPipe',
    })
    export class RemovehtmltagsPipe implements PipeTransform {
      /**
       * Takes a value and makes it lowercase.
       */
      transform(value: string) {
        if (value) {
          let result = value.replace(/<\/?[^>]+>/gi, "");
          return result;
        }
        else {
    
        }
      }
    }
    

    然后我将管道作为导出添加到组件的 module.ts 中

    details.module.ts 是这样的

    import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; 
    import { IonicPageModule } from 'ionic-angular';
    import { DetailsPage } from './details';
    import { RemovehtmltagsPipe } from 
    '../../pipes/removehtmltags/removehtmltags';
    
    @NgModule({
      declarations: [
        DetailsPage,
      ],
      imports: [
        IonicPageModule.forChild(DetailsPage),
      ],
      exports: [RemovehtmltagsPipe],
      schemas: [CUSTOM_ELEMENTS_SCHEMA]
    })
    export class DetailsPageModule {}
    

    终于在我的html代码中使用了管道

    details.html

     <ion-row class="white-bg" padding>
        <ion-col>
            <h1 class="title">{{article.title.rendered | RemovehtmltagsPipe}}</h1>
            <p class="date">Published: {{article.modified.split('T')[0]}}  {{article.modified.split('T')[1]}}</p>
        </ion-col>
    </ion-row>
    

    这应该会删除您帖子中的任何 html 标签。希望这会有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多