小编Cra*_*hax的帖子

打字稿:如何导出变量

我想打开'file1.ts'并写道:

export var arr = [1,2,3];
Run Code Online (Sandbox Code Playgroud)

并打开另一个文件,让我们说'file2.ts'并直接访问file1.ts中的'arr':

我是这样做的:

import {arr} from './file1';
Run Code Online (Sandbox Code Playgroud)

但是,当我想访问'arr'时,我不能只写'arr',但我必须写'arr.arr'.第一个是模块名称.如何直接访问导出的变量名称?

import module export typescript

40
推荐指数
2
解决办法
4万
查看次数

Angular 2:如何访问HTTP响应正文?

我在Angular 2中编写了以下代码:

this.http.request('http://thecatapi.com/api/images/get?format=html&results_per_page=10').
      subscribe((res: Response) => {
        console.log(res);
      })
Run Code Online (Sandbox Code Playgroud)

当我打印响应时,我进入控制台: 在此输入图像描述

我想在代码中访问响应中的body字段.'body'字段以下划线开头,这意味着它是一个私有字段.当我将其更改为'console.log(res._body)'时出错.

你知道任何可以帮助我的吸气功能吗?

javascript ajax http angular

39
推荐指数
6
解决办法
12万
查看次数

Enzyme需要配置适配器

我通过create-react-app创建了一个新的React应用程序,我想对我在应用程序中创建的名为"MessageBox"的组件编写单元测试.这是我写的单元测试:

import MessageBox from "../MessageBox";
import { shallow } from 'enzyme';
import React from 'react';

test('message box', () => {
   const app = {setState: jest.fn()};
   const wrapper = shallow(<MessageBox app={app}/>);
   wrapper.find('button').at(0).simulate('click');
   expect(app.setState).toHaveBeenLastCalledWith({modalIsOpen: false});
});
Run Code Online (Sandbox Code Playgroud)

我还在'src'文件夹下添加了一个名为'setupTests.js'的文件,其中包含以下内容:

import * as enzyme from 'enzyme';
import * as Adapter from 'enzyme-adapter-react-16';

enzyme.configure({ adapter: new Adapter() });
Run Code Online (Sandbox Code Playgroud)

我通过以下方式运行:

npm测试

我收到了错误:

酶内部错误:酶需要配置适配器,但没有找到.要配置适配器,您应该调用Enzyme.configure({ > adapter: new Adapter() })

你知道什么可以解决这个问题吗?

reactjs enzyme create-react-app

33
推荐指数
6
解决办法
2万
查看次数

Reactjs:如何将html模板放在一个单独的文件中?

我是React的新手.我对Angular2 +更熟悉.在Angular中,每个组件都有一个单独的html文件.但是,在React中,我看到render函数本身包含了html模板.例如,

import React, { Component } from 'react';

class HelloWorld extends Component {
    render() {
        return (
            <h2> Hello World </h2>
        );
    }
}

export default HelloWorld;
Run Code Online (Sandbox Code Playgroud)

好吧,我想接受

<h2> Hello World </h2>
Run Code Online (Sandbox Code Playgroud)

例如,在js文件之外并将其放在单独的html中并将html文件导入到渲染函数中

 render() {
            return (
                import content of helloworld.html
            );
        }
Run Code Online (Sandbox Code Playgroud)

你知道怎么做吗?

reactjs

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

BitBucket:主机密钥身份验证失败

我想将远程存储库克隆到本地计算机.我使用了命令:

git clone git@bitbucket.org:<username>/<repo_name>.git
Run Code Online (Sandbox Code Playgroud)

我收到了消息:

The authenticity of host 'bitbucket.org (104.192.143.3)' can't be
established. RSA key fingerprint is
SHA256:****. Are you sure you
want to continue connecting (yes/no)?  Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository
exists.
Run Code Online (Sandbox Code Playgroud)

我只想提一下,我已经处理了ssh问题.我通过命令' ssh-keygen' 生成了一个ssh密钥,并将内容复制~/.ssh/id_rsa.pubBitbucket Settings -> SSH keys(根据以下链接:https://confluence.atlassian.com/bitbucket/set-up-ssh-for-git-728138079.html)

我也查看了我的"用户和组访问",我看到了这个:

在此输入图像描述

你能告诉我该怎么办?

git ssh bitbucket

20
推荐指数
6
解决办法
2万
查看次数

Angular 2:没有(注入)服务的提供者

我的应用程序中有两个服务 - MainService和RightClickService.只有MainService可以在应用程序中全局访问,RightClickService将注入MainService.因此,我将以下文件定义为:

app.module.ts

@NgModule({
  declarations: [
    AppComponent,
    ...
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule
  ],
  providers: [MainService],
  bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

app.module.ts中未提及RightClickService.

service.ts

 @Injectable()
 export class MainService {

  constructor(private rightClickService: RightClickService) {
    console.log("Constructor is init");
  }
}
Run Code Online (Sandbox Code Playgroud)

RightClickService仅存在于大应用程序RightClickComponent内部命名的一个组件中.

右click.component.ts:

@Component({
  selector: 'right-click',
  template: `
    ...
  `,
  styleUrls: ['...'],
  providers: [RightClickService]
})
export class RightClickComponent implements OnInit {

  constructor(private rightClickService: RightClickService) {}

}
Run Code Online (Sandbox Code Playgroud)

不过,我收到错误:

EXCEPTION: No provider for RightClickService!
Run Code Online (Sandbox Code Playgroud)

你知道我做错了什么吗?

dependency-injection angular

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

Jest:如何模拟一个类的特定方法

我们假设我有以下课程:

export default class Person {
    constructor(first, last) {
        this.first = first;
        this.last = last;
    }
    sayMyName() {
        console.log(this.first + " " + this.last);
    }
    bla() {
        return "bla";
    }
}
Run Code Online (Sandbox Code Playgroud)

假设我想创建一个模拟类,其中方法'sayMyName'将被模拟,方法'bla'将保持原样.

我写的测试是:

const Person = require("../Person");

jest.mock('../Person', () => {
    return jest.fn().mockImplementation(() => {
        return {sayMyName: () => {
            return 'Hello'
        }};
    });
});


let person = new Person();
test('MyTest', () => {
    expect(person.sayMyName()).toBe("Hello");
    expect(person.bla()).toBe("bla");
})
Run Code Online (Sandbox Code Playgroud)

第一个'expect'语句通过,这意味着'sayMyName'被成功模拟.但是,第二个'期望'失败并出现错误:

TypeError:person.bla不是函数

据我所知,模拟类删除了所有方法.我想知道如何模拟一个类,只有特定的方法才会被模拟.

jestjs

15
推荐指数
6
解决办法
2万
查看次数

Java 8:如何编写 lambda 流以使用 JsonArray?

我对 Java 8 lambdas 和其他东西很陌生……我想编写一个 lambda 函数,它接受 a JSONArray,遍历它的JSONObjects 并创建某个字段的值列表。

例如,一个函数接受JSONArray:[{name: "John"}, {name: "David"}]并返回一个列表["John", "David"]

我写了以下代码:

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class Main {
    public static void main(String[] args) {
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(new JSONObject().put("name", "John"));
        jsonArray.add(new JSONObject().put("name", "David"));
        List list = (List) jsonArray.stream().map(json -> json.toString()).collect(Collectors.toList());
        System.out.println(list);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我收到一个错误:

线程“main”中的异常 java.lang.NullPointerException

你知道如何解决吗?

java lambda java-8 java-stream

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

无法删除IntelliJ/Cursive中的括号

我正在使用IntelliJ/Cursive来编写Clojure.我发现擦除括号的唯一方法是完全擦除它们内部的内容,然后才能删除括号.例如,假设我有以下代码:

(list)
Run Code Online (Sandbox Code Playgroud)

我想只删除左括号.一旦我在左括号上点击退格键,IDE就会忽略这个动作.只有当我删除单词"list"时,才能删除括号.

有谁知道如何解决这个问题?

clojure intellij-idea cursive

9
推荐指数
1
解决办法
2960
查看次数

Spring Boot:@Entity 注解是否存在?

我看了这个 Spring Boot 教程:https : //javabrains.io/courses/spring_bootquickstart/lessons/Creating-a-Spring-Data-JPA-Repository

并且它在类名之前放置了一个@Entity 注释。当我尝试在我的代码中执行此操作时,出现错误提示“无法解析 @Entity”。

这是我的 pom.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath/><!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>


    <dependencies>

        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.1.0</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin> …
Run Code Online (Sandbox Code Playgroud)

spring jpa spring-boot

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