小编Bee*_*ice的帖子

PhpStorm:如何判断数组成员的 var 类型?

我使用PhpStorm 2016.2.1IDE。我有一个类型化数组(所有成员都属于相同的已知类),我希望 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 函数: …

php ide intellisense phpdoc phpstorm

4
推荐指数
1
解决办法
527
查看次数

一些未知的东西将它添加到我的 htaccess 中。怎么办?

今天我注意到.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/空目录。我有两个问题。

  1. 我该怎么办?
  2. 重写条件有什么影响?我的重写似乎仍然像以前一样工作。

更新

这似乎与Let's Encrypt TLS 证书自动更新有关,因此该文件夹可能是由认证机器人创建的。但是为什么我要做这样的孵化工作.htaccess呢?正如我所说,我指出的三行在每次重写之前都会出现。所以它给文件增加了很多膨胀和混乱。

security .htaccess mod-rewrite

4
推荐指数
1
解决办法
1630
查看次数

Typescript中的Angular:如何将泛型Type传递给函数

使用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>,并在此处记录

generics typescript angular

4
推荐指数
1
解决办法
1929
查看次数

Angular 6:给定 ElementRef,我可以获得 ViewContainerRef

模板

<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给服务,它就会从中获取视图容器。

或者,可以ElementRefViewContainerRef? 似乎不可能,因为我假设单个视图容器可以有多个元素。

angular

4
推荐指数
1
解决办法
6202
查看次数

RxJS Observables:为什么回调会触发两次?

考虑来自 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管道中的操作符不应该创建twothree订阅相同的共享源吗?

来源 Stackblitz

rxjs angular rxjs6

4
推荐指数
1
解决办法
1622
查看次数

Angular 2:检测浏览器宽度的变化并更新模型

在我的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)

css sass rxjs typescript angular

3
推荐指数
1
解决办法
5788
查看次数

什么与我的Websocket连接断开连接?(cloudflare,apache的mod_proxy)

我的所有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在服务器上设置它可能是原因.我不知道如何深究这一点.

apache mod-proxy websocket cloudflare

3
推荐指数
1
解决办法
2608
查看次数

将MathJax集成到SystemJS构建中

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)

但是我正在尝试学习如何将库集成到我的构建设置中.

build mathjax systemjs angular

3
推荐指数
1
解决办法
349
查看次数

CSS:允许 textarea 随着内容增长到一个限制

在某些站点(例如 Github)上,textareas 有一组min-heightmax-height. 当用户输入它们时,它们会随着内容增长,直到达到最大高度。然后出现垂直滚动条。

这个效果是怎么实现的?我的 textarea 没有随着它的内容增长,即使它没有达到指定的max-height

textarea{
  width:100%;
  min-height:80px;
  max-height:200px;
  overflow:auto;
}
Run Code Online (Sandbox Code Playgroud)

看看这个演示

css

3
推荐指数
2
解决办法
2914
查看次数

Angular 6:带有自定义提供程序的 ViewContainerRef.createComponent

我的 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)

但这对我来说不起作用,因为如果可能的话,我需要在构建时立即提供容器引用。

angular

3
推荐指数
1
解决办法
5761
查看次数