简体   繁体   English

角度/打字稿? /Javascript? - “.default”是什么意思?

[英]Angular/ Typescript? /Javascript? - what does the ".default" mean?

Can someone explain what the ".default" means in the following code有人可以解释以下代码中“.default”的含义吗

I want to use such code in our project, but I am unsure about the code-fragment in question.我想在我们的项目中使用这样的代码,但我不确定有问题的代码片段。

(I've altered my question to show the original code.) (我已经改变了我的问题以显示原始代码。)

In the article angular-http-mock-interceptor-for-mocked-backend-1h5g at dev.io one can read code like this:在 dev.io 上的文章 angular-http-mock-interceptor-for-mocked-backend-1h5g 中,可以阅读如下代码:

import { Injectable, Injector } from '@angular/core';
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import * as users from './users.json';

const urls = [
    {
        url: 'https://an-example.url.org/users',
        json: users
    }
];

@Injectable()
export class HttpMockRequestInterceptor implements HttpInterceptor {
    constructor(private injector: Injector) {}

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        for (const element of urls) {
            if (request.url === element.url) {
                return of(new HttpResponse({ status: 200, body: ((element.json) as any).default })); // <---- THIS ".default"
            }
        }
    //...
    }
}

with users.json使用 users.json

[
 {
   "name": "Abe",
   "id": 1
 },
 {
   "name": "Boe",
   "id": 22
 }
]

I tried to google it, but with no success.我试图用谷歌搜索它,但没有成功。 I am not sure wether to try ot the code without the ".default" is sufficient enough, maybe I am missing something.我不确定是否尝试没有“.default”的代码就足够了,也许我错过了一些东西。 That's why I would appreciate it to get some knowledge about the meaning.这就是为什么我会很感激得到一些关于含义的知识。 Thank you in advance.先感谢您。

The reason for the default is because of the star import in here默认的原因是因为这里的星号导入

import * as users from './users.json';

You could make that import你可以做那个进口

import users from './users.json';

You will notice that changing the import breaks the code, so you need to remove that default.您会注意到更改导入会破坏代码,因此您需要删除该默认值。 and change that line to并将该行更改为

return of(new HttpResponse({ status: 200, body: element.json }));

console.log const url

Last thing is the reason you had to cast it is because json.element doesn't have that default property.最后一件事是您必须强制转换的原因是因为 json.element 没有该默认属性。

The previous image is just a console.log of the const urls.上一张图片只是 const url 的 console.log。

The stackblitz URL. stackblitz 网址。 https://stackblitz.com/edit/angular-mock-http-interceptor?file=src%2Fapp%2Finterceptor.mock.ts https://stackblitz.com/edit/angular-mock-http-interceptor?file=src%2Fapp%2Finterceptor.mock.ts

I am confused about the order. It is first "cast" to any, and then the default-property is accessed. I don't understand why

A more natural way that you might think about is to assign the url type to any and that does work.您可能会想到的一种更自然的方法是将 url 类型分配给 any 并且确实有效。

const urls:any = [
    {
        url: 'https://jsonplaceholder.typicode.com/users',
        json: users
    }
];

@Injectable()
export class HttpMockRequestInterceptor implements HttpInterceptor {
    constructor(private injector: Injector) {}

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        for (const element of urls) {
            if (request.url === element.url) {
                console.log('Loaded from json : ' + request.url);
                return of(new HttpResponse({ status: 200, body: element.json.default }));
            }
        }
        console.log('Loaded from http call :' + request.url);
        return next.handle(request);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM