如何正确地为JS导入别名?

时间:2018-04-18 09:31:22

标签: javascript reactjs

我有基本的反应类:

import React, { Component } from 'react';

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

export default Children;

位于/scenes/Stash/Children/Children。我希望importStashChildren

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

但我明白了:

  

45:88-101“出口'儿童'(导入'StashChildren')不是   发现在'_ / scenes / Stash / Children / Children'

如果我这样做:

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

一切正常。

3 个答案:

答案 0 :(得分:1)

您可以通过

导入默认导出

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

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

您只需要在所需名称下导入默认值。

答案 1 :(得分:0)

由于您已将子组件导出为默认导出,因此可以使用任何名称导入它。所以你只需要

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

查看此When should I use brackets with imports了解详情

答案 2 :(得分:0)

由于您要导出默认值,因此可以调用 import Children as StashChildren from './scenes/Stash/Children/Children';

对于别名,只需删除括号:

class SwapValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) unless value.present? || record.attr3.present? record.errors.add(attribute, :invalid) end end end