javascript - 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>
有什么想法吗?
解决方案
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>
...
..
推荐阅读
- cad - 带有图像到位图的 Cadlib 绘制模型
- javascript - 想要为嵌套创建正则表达式以逃避嵌套括号
- ios - UISearchController 横向布局问题
- c# - 获取旋转图像上的点击坐标
- python - xlsx 表的 reader = csv.reader(...) 的等价物是什么?
- oracle - Oracle DB 中 ENUM 列的 Liquibase 'addCheckConstraint' 允许任何字符
- python - 用正则表达式捕捉第一场比赛 - PYTHON
- sql - 带有一个额外的自定义条件列的 Oracle 查询
- mysql - MySQL左连接中的语法问题
- python-3.x - None appereance in a if statment