我使用PhpStorm 2016.2.1
IDE。我有一个类型化数组(所有成员都属于相同的已知类),我希望 IDE 知道该类型,以便它可以帮助我解决自动完成/智能感知问题。
class MyClass{
/**
* @var array
*/
public $userArray;
public addUser($uid){ $this->$userArray[$uid] = new User($uid); }
public processUser($uid){
$oUser = $this->$userArray[$uid];
//since the PHP array can contain anything, the IDE makes
//no assumption about what data type $oUser is. How to let it
//know that it's of type User?
}
}
Run Code Online (Sandbox Code Playgroud)
我努力了...
/**
* @var User
*/
public $oUser = ...;
Run Code Online (Sandbox Code Playgroud)
并且
/**
* @type User
*/
public $oUser = ...;
Run Code Online (Sandbox Code Playgroud)
到目前为止,我唯一要做的就是使用 getter 函数: …
今天我注意到.htaccess
我的public_html
根目录中的文件在几个月前被修改了。
在每一RewriteRule
行之前,某个机器人或某人添加了以下三行:
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
# lines above were inserted above each rewrite such as the following
RewriteRule ^home/? /index.html [QSA,END,NC]
Run Code Online (Sandbox Code Playgroud)
我还注意到我有一个public_html/.well-known/acme-challenge/
空目录。我有两个问题。
这似乎与Let's Encrypt TLS 证书自动更新有关,因此该文件夹可能是由认证机器人创建的。但是为什么我要做这样的孵化工作.htaccess
呢?正如我所说,我指出的三行在每次重写之前都会出现。所以它给文件增加了很多膨胀和混乱。
使用Typescript编写Angular 5单元测试,我有一个函数将查询DOM并返回MyComponent
该类的实例(如果找到):
function getMyComponent(hostFixture: ComponentFixture<any>): MyComponent {
const debugElement = hostFixture.debugElement.query(By.directive(MyComponent));
return (debugElement && debugElement.componentInstance) || null;
}
Run Code Online (Sandbox Code Playgroud)
这完美无瑕.现在我想概括一下这个函数,以便它可以按需查询任何类型.我认为这可以用泛型类型完成.函数将被调用如下:
const instance: MyComponent = getDirective<MyComponent>(fixture);
Run Code Online (Sandbox Code Playgroud)
我不能让它工作; 这是破碎的功能:
function getDirective<T>(hostFixture: ComponentFixture<any>): T {
const debugElement = hostFixture.debugElement.query(By.directive(T));
return (debugElement && debugElement.componentInstance) || null;
}
Run Code Online (Sandbox Code Playgroud)
函数中的第一行抛出错误:
TS2693:'T'仅指类型,但在此处用作值
这里的语法是什么?我正在调用的Angular函数By.directive()
- 采用类型的参数Type<any>
,并在此处记录
模板
<div my-directive></div>
Run Code Online (Sandbox Code Playgroud)
指示
constructor(vc: ViewContainerRef, el: ElementRef, svc: MyService) {}
useService() {
this.svc.doWork(vc, el);
}
Run Code Online (Sandbox Code Playgroud)
因此该服务需要 aViewContainerRef
和 an ElementRef
。我必须两者都通过吗?有没有办法从元素引用派生视图容器?如果是这样,我只需要传递ElementRef
给服务,它就会从中获取视图容器。
或者,可以ElementRef
从ViewContainerRef
? 似乎不可能,因为我假设单个视图容器可以有多个元素。
考虑来自 Angular 6 组件的这段代码:
class AppComponent {
subject = new Subject<any>();
one = this.subject.pipe(share(), tap(e => log('one emits')));
two = this.one.pipe(tap(e => log('two emits')));
three = this.one.pipe(delay(500), tap(e => log('three emits')));
ngOnInit() {
this.two.subscribe(e => log('two received'));
this.three.subscribe(e => log('three received'));
this.subject.next();
}
}
Run Code Online (Sandbox Code Playgroud)
执行时ngOnInit
,记录的内容如下:
一个发出
两个发射
两个收到
一个发出
三发射
三个收到
我不明白:为什么one
发射两次?share
管道中的操作符不应该创建two
并three
订阅相同的共享源吗?
在我的Angular 2
应用程序中,我根据浏览器窗口宽度更改样式.这是一个示例(SCSS):
.content{
/*styles for narrow screens*/
@media (max-width: 750px){
background-color:beige;
}
/*styles for medium screens*/
@media (min-width: 751px) and (max-width: 1200px) {
background-color:red;
}
/*styles for wide screens*/
@media (min-width: 1201px) {
background-color:brown;
}
}
Run Code Online (Sandbox Code Playgroud)
这使我的UI响应.我希望我的Angular组件知道哪个宽度间隔是最新的:
/* Returns which of the CSS width intervals is current*/
getWidthRange(){
let pixelWidth = ...; //what goes here?
if(pixelWidth < 251) return "small";
if(pixelWidth < 451) return "medium";
return "large";
}
Run Code Online (Sandbox Code Playgroud)
组件将调用此函数并根据宽度调整其行为.例如,下面的模板将在更宽的屏幕上显示更多内容:
<div>
{{getWidthRange()==='small'? shortText : longText}}
</div>
Run Code Online (Sandbox Code Playgroud)
更好的是,我会设置一个Observable,当浏览器从一个范围转换到下一个范围时发出:
widthRangeChanges = …
Run Code Online (Sandbox Code Playgroud) 我的所有websocket连接都通过http(s)端口到达并代理到后端WS服务器:
[client]----[cloudflare]----[Apache 2.4 mod_proxy_wstunnel]----[websocket server]
Run Code Online (Sandbox Code Playgroud)
一旦客户端连接到我的WS服务器,如果没有数据通过套接字,则连接总是在100秒后被切断.
在开发环境中,使用相同的客户端,也使用mod_proxy_wstunnel
相同的WS服务器,不会发生此限制
如果WS服务器每60秒发送一次ping,则不会切断连接.
我想知道是否有人看过有关Cloudflare断开静默WS连接的文档,以及是否mod_proxy
在服务器上设置它可能是原因.我不知道如何深究这一点.
我SystemJS
用来构建一个Angular 2
应用程序,我想开始MathJax
在组件中使用.我安装了:
npm install --save-dev mathjax
npm install --save @types/mathjax
Run Code Online (Sandbox Code Playgroud)
MathJax现已安装在/node_modules/mathjax
.当我的组件执行时import "mathjax"
,文件/node_modules/mathjax/MathJax.js
已加载.到现在为止还挺好.
问题是该文件然后尝试使用相对于项目根目录而不是MathJax安装目录的路径加载其他文件.
例如,MathJax.js
尝试加载
/extensions/MathMenu.js 404 Not found
/extensions/MathZoom.js 404 Not found
Run Code Online (Sandbox Code Playgroud)
正确的路径是
/node_modules/mathjax/extensions/MathMenu.js
/node_modules/mathjax/extensions/MathZoom.js
Run Code Online (Sandbox Code Playgroud)
快速查看源文件内部显示URL是这样引用的:
[MathJax]/extensions/MathMenu.js
Run Code Online (Sandbox Code Playgroud)
知道我应该在哪里解决这个问题吗?我知道我不应该更改MathJax.js
文件本身,因为更新后将覆盖任何更改.使用htaccess
或其他服务器端重定向也没有意义.
我知道我可以使用CDN加载一个有效的MathJax包:
<script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=AM_SVG"></script>
Run Code Online (Sandbox Code Playgroud)
但是我正在尝试学习如何将库集成到我的构建设置中.
在某些站点(例如 Github)上,textareas 有一组min-height
和max-height
. 当用户输入它们时,它们会随着内容增长,直到达到最大高度。然后出现垂直滚动条。
这个效果是怎么实现的?我的 textarea 没有随着它的内容增长,即使它没有达到指定的max-height
textarea{
width:100%;
min-height:80px;
max-height:200px;
overflow:auto;
}
Run Code Online (Sandbox Code Playgroud)
我的 Angular 6 服务动态创建一个组件:
buildComponent(viewContainerRef) {
const factory = this.componentFactoryResolver.resolveComponentFactory(MyComponent);
const componentRef = viewContainerRef.createComponent(componentFactory);
...
}
Run Code Online (Sandbox Code Playgroud)
我正在创建的组件需要viewContainerRef
在其构造函数中包含一个句柄。创建组件时如何从服务传递它?有没有办法自定义createComponent
执行时使用的注入器?(提示:我注意到这injector
是 的参数之一createComponent
)
我尝试将容器传递给下一行的组件,如下所示:
componentRef.instance.viewContainer = viewContainerRef;
Run Code Online (Sandbox Code Playgroud)
但这对我来说不起作用,因为如果可能的话,我需要在构建时立即提供容器引用。