简体   繁体   English

如何正确地在JS中导入别名?

[英]How to properly do alias for import in JS?

I have basic react class: 我有基本的反应课:

import React, { Component } from 'react';

class Children extends Component {
    render() {
        return (
            <h1>Children</h1>
        );
    }
}

export default Children;

Which is located at /scenes/Stash/Children/Children . 位于/scenes/Stash/Children/Children I want to import it like StashChildren . 我想像StashChildren一样import它。

import {Children as StashChildren} from './scenes/Stash/Children/Children';

But I get: 但是我得到:

45:88-101 "export 'Children' (imported as 'StashChildren') was not found in '_/scenes/Stash/Children/Children' 45:88-101“在'_ / scenes / Stash / Children / Children'中找不到导出'Children'(作为'StashChildren'导入)

If I do just: 如果我只是做:

import {Children} from './scenes/Stash/Children/Children';

Everything works fine. 一切正常。

You can import the default export by either 您可以通过以下任一方式导入默认导出

import StashChildren from './scenes/Stash/Children/Children'

or 要么

import {default as StashChildren} from './Children';

You just need to import the default under the name that you want. 您只需要在所需名称下导入默认值即可。

Since you have exported children component as a default export you can import it by any name. 由于已将子组件导出为默认导出,因此可以使用任何名称导入子组件。 So you simply need 所以你只需要

import StashChildren from './scenes/Stash/Children/Children';

Check this When should I use brackets with imports for more details 选中此选项何时应在导入中使用方括号以了解更多详细信息

Since you're exporting default you can call like import StashChildren from './scenes/Stash/Children/Children'; 由于您要导出默认值,因此可以import StashChildren from './scenes/Stash/Children/Children';调用import StashChildren from './scenes/Stash/Children/Children';

For alias, just remove the brackets: 对于别名,只需除去括号:

import Children as StashChildren from './scenes/Stash/Children/Children';

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

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