创建自定义的社交分享按钮在Angular中

huangapple go评论90阅读模式
英文:

Create Custom Social Share Buttons in angular

问题

I will provide a translation of the content you provided:

我会提供你提供的内容的翻译:

I will appreciate any ideas and support for this issue
我将感激对这个问题的任何想法和支持

I have an angular project and I try to create icons share buttons for social media.
我有一个Angular项目,我尝试创建用于社交媒体分享的图标按钮。

I have a component named landing.component. in the HTML file I implemented a Modal popup to allow showing a new windows where I have the icons. Please, follow screenshots below
我有一个名为landing.component的组件。在HTML文件中,我实现了一个模态弹窗,以便显示一个新窗口,其中包含这些图标。请查看下面的截图。

创建自定义的社交分享按钮在Angular中
创建自定义的社交分享按钮在Angular中

Below, is the new windows
下面是新窗口

创建自定义的社交分享按钮在Angular中
创建自定义的社交分享按钮在Angular中

the HTML code is here
这里是HTML代码

(以下是HTML代码部分)

In the facebook option, I created a variable #facebook that will allow me to use viewchildren in the ts file to do an elementref and find the element in the DOM.
在Facebook选项中,我创建了一个变量#facebook,这将允许我在ts文件中使用viewchildren来执行elementref并查找DOM中的元素。

In the viewchildren option, I created a variable link1.
在viewchildren选项中,我创建了一个变量link1。

then, in the ngAfterViewInit I created two variables url and title. the url variable will take the link of my page to be share and the title is just a message.
然后,在ngAfterViewInit中,我创建了两个变量url和title。url变量将获取我的页面链接以供分享,而title只是一条消息。

Finally, I used this command:
最后,我使用了这个命令:

(以下是命令部分)

The code is not working, basically when I click the Facebook icon, it only closes the window and that's it, and the web shows me this error here below:
代码不起作用,基本上当我点击Facebook图标时,它只是关闭窗口,就是这样,网页显示我下面的错误:

(以下是错误截图部分)

the landing.component.ts code is here below:
landing.component.ts代码如下:

(以下是代码部分)

I really appreciate any help.
我非常感激任何帮助。

英文:

I will appreciate any ideas and support for this issue

I have an angular project and I try to create icons share buttons for social media.
I have a component named landing.component. in the HTML file I implemented a Modal popup to allow showing a new windows where I have the icons. Please, follow screenshots below

创建自定义的社交分享按钮在Angular中

Below, is the new windows

创建自定义的社交分享按钮在Angular中

the HTML code is here

  1. <div class="card-wrapper" fxLayout="row wrap" fxLayoutAlign="start start">
  2. <div
  3. class="card active-card"
  4. fxFlex="25"
  5. *ngFor="let category of categories"
  6. (click)="categoryClick(category)"
  7. >
  8. <picture>
  9. <img [src]="category.imageLocation" />
  10. </picture>
  11. <div class="card-overlay" fxLayout="row wrap" fxLayoutAlign="center end">
  12. <span class="card-title" fxFlex="100">{{ category.title }}</span>
  13. <div class="card-buttons" fxFlex="100" fxLayout="row">
  14. <ul class="list-inline text-center member-icons ">
  15. <li class="list-inline-item">
  16. <button mat-icon-button disableRipple class="btn btn-primary" routerLink="/mtl" (click)="open(content)"><i class="fa-solid fa-share"></i></button>
  17. </li>
  18. <span fxFlex></span>
  19. </ul>
  20. <!-- <button mat-icon-button disableRipple>
  21. <heart-icon></heart-icon>
  22. </button> -->
  23. <!-- <span fxFlex></span> -->
  24. <!-- <button mat-icon-button disableRipple> -->
  25. <!-- <download-icon></download-icon> -->
  26. <!-- </button> -->
  27. <!-- <button mat-icon-button disableRipple>
  28. <share-icon></share-icon>
  29. </button> -->
  30. </div>
  31. </div>
  32. </div>
  33. </div>
  34. <section
  35. class="cragcity-container p-t-5"
  36. fxLayout="row"
  37. fxLayoutAlign="space-between start"
  38. >
  39. <div style="margin: auto">
  40. <img class="socmed-icon m-r-15" src="./assets/icons/facebook.webp" />
  41. <img class="socmed-icon m-r-15" src="./assets/icons/skype.webp" />
  42. <img class="socmed-icon" src="./assets/icons/instagram.webp" />
  43. </div>
  44. </section>
  45. <ng-template #content let-modal>
  46. <div class="modal-header">
  47. <h4 class="modal-title" id="modal-basic-title">Share Social Media</h4>
  48. <button type="button" class="btn-close" aria-label="Close" (click)="modal.dismiss('Cross click')"></button>
  49. </div>
  50. <div class="modal-body">
  51. <form>
  52. <div class="mb-3">
  53. <label for="Social Media"></label>
  54. <div class="share-btn-container">
  55. <a #facebook href="#" class="facebook-btn">
  56. <i class="fab fa-facebook"></i>
  57. </a>
  58. <a href="#" class="twitter-btn">
  59. <i class="fab fa-twitter"></i>
  60. </a>
  61. <a href="#" class="pinterest-btn">
  62. <i class="fab fa-pinterest"></i>
  63. </a>
  64. <a href="#" class="linkedin-btn">
  65. <i class="fab fa-linkedin"></i>
  66. </a>
  67. <a href="#" class="whatsapp-btn">
  68. <i class="fab fa-whatsapp"></i>
  69. </a>
  70. </div>
  71. <!-- <div class="input-group">
  72. <input
  73. name="dp"
  74. ngbDatepicker
  75. #dp="ngbDatepicker"
  76. />
  77. <button class="btn btn-outline-secondary bi bi-calendar3" (click)="dp.toggle()" type="button"></button>
  78. </div> -->
  79. </div>
  80. </form>
  81. </div>
  82. <div class="modal-footer">
  83. <button type="button" class="btn btn-outline-dark" (click)="modal.close('Save click')">Close</button>
  84. </div>
  85. </ng-template>
  86. <app-cragcity-footer></app-cragcity-footer>

In the facebook option, I created a variable #facebook that will allow me to use viewchildren in the ts file to do an elementref and find the element in the DOM.
In the viewchildren option, I created a variable link1.

then, in the ngAfterViewInit I created two variables url and title. the url variable will take the link of my page to be share and the title is just message.
Finally, I used this command:

  1. this.link1.nativeElement.setAttribute("href",`https://www.facebook.com/sharer.php?u=${this.url}`)

(this is what I believe is the same in js document.location.href) but used in ts

The code is not working, basically when I click the facebook icon only close the windows and that's it and the web shows me this error here bbelwo:

创建自定义的社交分享按钮在Angular中

the landing. component.ts code is here below:

  1. import { Component, OnInit, Input, ViewChild, ElementRef, AfterViewInit, ViewChildren } from '@angular/core';
  2. import { Router, ActivatedRoute } from '@angular/router';
  3. import { DataService } from 'src/app/core/services/data.service';
  4. import { category } from '../models/category';
  5. import { ModalDismissReasons, NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap';
  6. @Component({
  7. selector: 'app-landing',
  8. templateUrl: './landing.component.html',
  9. styleUrls: ['./landing.component.scss']
  10. })
  11. export class LandingComponent implements OnInit, AfterViewInit {
  12. public categories = new Array<category>();
  13. @Input() name;
  14. url:string;
  15. title:string;
  16. closeResult: string | undefined;
  17. constructor(private _dataService: DataService, private router: Router, private modalService: NgbModal, private route: ActivatedRoute) {
  18. }
  19. ngOnInit(): void {
  20. this.retrieveCategories();
  21. }
  22. @ViewChildren("facebook")link1: ElementRef;
  23. ngAfterViewInit() {
  24. this.url = this.router.url;
  25. /* this.link1.nativeElement = this.url; */
  26. this.title = encodeURI("Facebook");
  27. this.link1.nativeElement.setAttribute("href",`https://www.facebook.com/sharer.php?u=${this.url}`)
  28. /* console.log(this.link1);
  29. console.log(this.url);
  30. */
  31. /* this.link1.nativeElement.setAttribute('xlink:href') */
  32. }
  33. retrieveCategories() {
  34. const actionUrl = 'v1/category';
  35. const params = {};
  36. this._dataService.get<any>(actionUrl, params)
  37. .subscribe(data => {
  38. if (data !== null) {
  39. if (!data.isError) { this.categories = data.result; }
  40. }
  41. });
  42. }
  43. categoryClick(data: category) {
  44. if (data.subCategory.length > 0)
  45. this.router.navigate(['mtl', data.id]);
  46. }
  47. open(content) {
  48. this.modalService.open(content, { ariaLabelledBy: 'modal-basic-title' }).result.then(
  49. (result) => {
  50. this.closeResult = `Closed with: ${result}`;
  51. },
  52. (reason) => {
  53. this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
  54. },
  55. );
  56. }
  57. private getDismissReason(reason: any): string {
  58. if (reason === ModalDismissReasons.ESC) {
  59. return 'by pressing ESC';
  60. } else if (reason === ModalDismissReasons.BACKDROP_CLICK) {
  61. return 'by clicking on a backdrop';
  62. } else {
  63. return `with: ${reason}`;
  64. }
  65. }
  66. }

I really appreciate any help

Regards

答案1

得分: 1

以下是翻译好的部分:

首先,@ViewChild@ViewChildren 之间有一个区别。前者将查找一个元素,而后者将查找多个元素。所以在您的情况下,使用 @ViewChild("facebook") link1: any;

但更好的方法是让 Angular 完成这项工作,而不要使用 setAttribute 来操作原生元素。这样,您可以像这样绑定任何内容到 href 属性:

HTML

  1. <a #facebook [href]="facebookLink" class="facebook-btn">
  2. <i class="fab fa-facebook"></i>
  3. </a>

代码

  1. facebookLink: string = "https://facebook.com";

您还可以使用 ngFor 循环生成链接,以获得更大的灵活性。

代码

  1. linkObjects = [
  2. { iconClassI: "fab fa-facebook", iconClassA: "facebook-btn", link: "http://facebook.com" },
  3. {... 其他所有链接}
  4. ]

HTML

  1. <div class="share-btn-container">
  2. <a *ngFor="let item of linkObjects" [href]="item.link" [class]="item.iconClassA">
  3. <i [class]="item.iconClassI"></i>
  4. </a>
  5. </div>

希望这有帮助。如果您有任何其他问题,请随时提问。

英文:

First it is a different between @ViewChild and @ViewChildren. The first will search one element, the second multiple elements. So in your case use @ViewChild(&quot;facebook&quot;) link1: any;

But a better approach is to let Angular do this thing and don't use setAttribute to the native element. So you can bind anything to the href attribute like this:

HTML

  1. &lt;a #facebook [href]=&quot;facebookLink&quot; class=&quot;facebook-btn&quot;&gt;
  2. &lt;i class=&quot;fab fa-facebook&quot;&gt;&lt;/i&gt;
  3. &lt;/a&gt;

Code

  1. facebookLink: string = &quot;https://facebook.com&quot;;

You can generate the links with a ngFor loop, too for more flexibility.

CODE

  1. linkObjects = [{iconClassI: &quot;fab fa-facebook&quot;, iconClassA: &quot;facebook-btn&quot;, link: &quot;http://facebook.com&quot;}, {... all other links}]

HTML

  1. &lt;div class=&quot;share-btn-container&quot;&gt;
  2. &lt;a *ngFor=&quot;let item of linkObjects&quot; [href]=&quot;item.link&quot; [class]=&quot;item.iconClassA&quot;&gt;
  3. &lt;i [class]=&quot;item.iconClassI&quot;&gt;&lt;/i&gt;
  4. &lt;/a&gt;
  5. &lt;/div&gt;

huangapple
  • 本文由 发表于 2023年2月14日 01:49:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定