小编Dar*_*lyn的帖子

promise.all如何运作?

我在承诺中开始挖掘,发现了有趣的Promise.all.

在MDN中说明了这一点

Promise.all(iterable)方法返回一个promise,该promise在迭代参数中的所有promise都已解析时解析.

这基本上意味着设置promises会在之后解析并且如果参数列表中的所有promise都已解决.我试图实现它.我做了简单的承诺ajax电话.

var get = function(url) {
    return new Promise(function(resolve,reject) {
        var xhtml=new XMLHttpRequest();

        xhtml.open("GET",url);
        xhtml.responseType = 'blob';
        xhtml.onload = function() {
            if(xhtml.status==200){
                resolve(xhtml.response);
            } else {
                reject(Error("Error"+statusText));
            }
        }
        xhtml.send();
    });

}

get("one.jpg").then(function(response){
    var blob = window.URL.createObjectURL(response);
    var img = document.createElement("img");

    console.log("Success"+response);

    img.src = blob;

    document.body.appendChild(img);
});
Run Code Online (Sandbox Code Playgroud)

哪个工作正常.但在我尝试添加Promise.all之后,它抛出了一个错误.

Promise.all(get).then(function(response){alert("done")});
Run Code Online (Sandbox Code Playgroud)

这就像我说的那样抛出一个错误"Promise.all的参数1无法转换为序列." 所以我假设我没有得到promise.all的含义.它是如何工作的?

javascript promise es6-promise

6
推荐指数
1
解决办法
4832
查看次数

在Hostlistener上滚动事件

我已经定义了模板

@Component({
    selector: 'name',
    directives: [ ... ],
    templateUrl: 'name.html'
})
Run Code Online (Sandbox Code Playgroud)

和班级

export class ProductGridComponent implements OnInit {
    @HostListener('scroll', ['$event'])
    onScroll(e) {
        alert(window.pageYOffset)
    }

    products = [];
}
Run Code Online (Sandbox Code Playgroud)

但它没有拍摄任何东西,但是当我用click和onClick替换scroll和onScroll时它确实显示了警告.

为什么它不能用于滚动,angular2是否有任何其他实现呢?

谢谢

angular

6
推荐指数
5
解决办法
3万
查看次数

Treewalker迭代

是否可以跳过当前子树中的迭代并使用treewalker跳转到下一个节点?例

<nav>
    <p>paragraph</p>
    <ul>
        <li>one</li>
        <li>two</li>
    </ul>
    <p>paragraph</p>
</nav>
Run Code Online (Sandbox Code Playgroud)

和js

var nav=document.getElementsByTagName("nav")[0];
    var tree=document.createTreeWalker(nav,NodeFilter.SHOW_ELEMENT,null,false);
    tree.firstChild(); // first paragraph
    tree.nextSibling(); // ul
    tree.firstChild(); // first li chid of ul
    tree.nextNode()||tree.nextSibling() // both return next li
Run Code Online (Sandbox Code Playgroud)

有没有办法如何停止子树的迭代,并在treewalker点击第一个LI元素后直接跳到另一个段落?

javascript

5
推荐指数
1
解决办法
1149
查看次数

在javascript中使用链表

在javascript中使用链表有什么好处吗?它对数组的主要优点(例如)是我们可以在不移动每个元素的情况下在随机索引处插入元素,并且它们不受数组大小的限制。

但是,JS 中的数组是动态扩展、收缩的,数组访问数据的速度更快。我们也可以使用Array.prototype.splice()方法(确实链表可能比这个更快)来插入数据。

那么在 JavaScript 中使用链表而不是数组有什么优势(速度等)吗?

使用 JS 的基本链表代码。

function list() {

  this.head = null;
  this.tail = null;

  this.createNode=function(data) {
    return {data: data, next: null }
  };

  this.addNode=function(data) {
    if (this.head == null) {
      this.tail = this.createNode(data);
      this.head = this.tail;
    } else {
      this.tail.next = this.createNode(data);
      this.tail = this.tail.next;
    }
  };

  this.printNode=function() {
    var x = this.head;
    while (x != null) {
      console.log(x.data);
      x = x.next;
    }
  }
}

var list = new list();
list.addNode("one");
list.addNode("two");
list.printNode();
Run Code Online (Sandbox Code Playgroud)

javascript arrays linked-list

5
推荐指数
1
解决办法
1447
查看次数

ngIf 检查元素上是否存在类

有没有办法使用 *ngIf 来检查元素是否具有某个类?我尝试使用

<img *ngIf="[class.imgView]" class="imgView" src="..">
Run Code Online (Sandbox Code Playgroud)

抛出错误无法读取未定义的属性 imgView。

有什么办法可以用角度来实现吗?

angular

5
推荐指数
1
解决办法
2万
查看次数

CLion 无法识别 c++17 功能

我正在使用 CLion 和 c++17 项目,我正在尝试使用

std::map<int, std::string> m{ {10, "potato"}, {1, "banana"} };
auto nodeHandler = m.extract(10);
Run Code Online (Sandbox Code Playgroud)

然而 CLion 一直抱怨摘录不是地图的成员。

我如何强制 CLion 使用/识别 c++17 功能?

CMake:

cmake_minimum_required(VERSION 3.12)
project(untitled2)

set(CMAKE_CXX_STANDARD 17)

add_executable(untitled2 main.cpp)
Run Code Online (Sandbox Code Playgroud)

c++ clion c++17

5
推荐指数
0
解决办法
2372
查看次数

允许模板参数仅为特定类型并基于它来决定操作

假设我有两节课MyClass_one,MyClass_two

我的功能只接受它们作为第一个参数

template<typename T,typename ...Ts>
void doSomething(T one, Ts...two){}
Run Code Online (Sandbox Code Playgroud)

现在为了简单起见,如果参数oneMyClass_one它应该打印"im one"如果MyClass_two它应该打印"im two".

如何实际实现这一目标?我提出的唯一解决方案是非常丑陋,并没有包含编译错误抛出:

template<typename T> isOne{ static const bool value = false}
template<> isOne<MyClass_one>{ static const bool value = true}

template<typename T> isTwo{ static const bool value = false}
template<> isTwo<MyClass_two>{ static const bool value = true}

template<typename T, typename ... Ts>
void doSomething(T one, Ts...two){
  if( isOne<T>::value ) { cout << "im one" << endl;}
  else if ( isTwo<T>::value){ cout …
Run Code Online (Sandbox Code Playgroud)

c++ templates

5
推荐指数
1
解决办法
132
查看次数

用节点替换大括号和文本

我有一个字符串

var str="Hello my name is {john/www.john.com} and welcome to my {site/www.site.com}."
Run Code Online (Sandbox Code Playgroud)

我已经提取了大括号,并用它们制作了一个锚标记

<a href="www.john.com">john</a>
Run Code Online (Sandbox Code Playgroud)

我想要做的是用这些节点替换大括号和内容.是否可以使用regExp?我已经研究过MDN上的regExp,但仍然无法弄清楚方法.

javascript regex

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

如果未使用spring-boot设置“ hibernate.dialect”,则对DialectResolutionInfo的访问不能为空

我正在尝试在我的spring-boot项目中配置休眠模式。

我有application.properties文件

spring.datasource.url=jdbc:mysql:url?useSSL=false
spring.datasource.username=name
spring.datasource.password=password

hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql:url
hibernate.connection.username = name
hibernate.connection.password = password
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
hibernate.dialect=org.hibernate.dialect.MySQLDialect
Run Code Online (Sandbox Code Playgroud)

以及在UserDAO类中注册用户的方法:

public void registerUser(User u){
    Session session = factory.openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        session.save(u);
        tx.commit();
    } catch (HibernateException e) {
        if (tx!=null) tx.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }
    System.out.println("yay all done");
}
Run Code Online (Sandbox Code Playgroud)

但是我收到错误:

> Caused by: org.hibernate.service.spi.ServiceException: Unable to
> create requested service
> [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]   at
> org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:275)
> ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]     at
> …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate

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

附加到画布内对象的事件

我只是在画布上绘制rect的画布代码

var x=document.getElementById("canvas");
var ctx=x.getContext("2d");
ctx.rect(20,20,150,100);
ctx.stroke();
Run Code Online (Sandbox Code Playgroud)

可以在所说的rect上添加eventListener吗?例如,如果我点击rect,它将变为红色.

javascript canvas dom-events html5-canvas

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