首页 > 解决方案 > Angular - 带有窗口对象的 Twitter 直接消息在 Safari 中不起作用

问题描述

我想用预先填充的文本撰写 Twitter DM。目前,我正在使用 window.open() 来撰写消息。

窗口.打开(https://twitter.com/messages/compose?text=${this.helloWorld});

helloWorld = "测试测试"

它在 Windows 和 android 上运行良好,但我无法在 iPhone 上获取预填充的文本。

这是代码:

handleSocialClick(){ window.open(https://twitter.com/messages/compose?text=${this.helloWorld}) }
<a (click)="handleSocialClick()" [ngClass]="socialOption.panelClass"> 
    <i [ngClass]="socialOption.icon"></i> {{socialOption.name}} </a> 

有什么想法吗?

标签: javascriptiosangular

解决方案


window.open由于浏览器策略的变化,您无法安全地依赖。

为了window.open()在 iOS 上的 safari 中工作,您可以将其放置在元素的onclick属性中。

这是一个带有按钮的示例,但您可以onclick在您使用的任何容器元素上放置一个事件:

<button class='button' onclick='window.open("https://twitter.com/messages/compose?text=${this.helloWorld}", "_blank");'>Twitter Message</button>

如果您使用异步调用来检索您的网址 - 请记住:Safari 会阻止对window.open()内部异步调用的调用。解决方法是window.open在 asnyc 调用之前调用并在 promise 解决后设置位置。

例子:

var myWindowRef = window.open();

someService.getUrl().then(function(url) {
     myWindowRef .location = url;
});

关于您的具体情况:

在 yourcomponent.ts 中:

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})


export class AppComponent  {
  ...
  ..
  helloWorld = 'Hello World';
  handleSocialClick(){ 
   window.open(`https://twitter.com/messages/compose?text=${this.helloWorld}`) 
 }

}

在 yourcomponent.html 中:

...
..
<a (click)="handleSocialClick()" [ngClass]="socialOption.panelClass"> 
    <i [ngClass]="socialOption.icon"></i> {{socialOption.name}} </a> 
...
..


推荐阅读