导入模块时'as'是什么意思?

时间:2017-03-20 18:34:36

标签: javascript reactjs

导入多个内容时的含义是什么?例如,

<dict>
    <key>NSExtensionAttributes</key>
    <dict>
        <key>NSExtensionActivationRule</key>
        <string>
        SUBQUERY (
            extensionItems,
            $extensionItem,
            SUBQUERY (
                $extensionItem.attachments,
                $attachment,
                ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url" ||
                ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.plain-text" ||
                ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.text" ||
                ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.jpeg" ||
                ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.png"  ||
                ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.image" ||
                ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.adobe.pdf"
            ).@count == 1
        ).@count == 1
        </string>
        <key>NSExtensionJavaScriptPreprocessingFile</key>
        <string>GetURL</string>
    </dict>
    <key>NSExtensionPointIdentifier</key>
    <string>com.apple.share-services</string>
    <key>NSExtensionMainStoryboard</key>
    <string>MainInterface</string>
</dict>

这只是来自react路由器的示例,javascript文档仅显示'as'之后的一个声明的示例

它看起来像是将BrowserRouter导入为路由器,路由和链接,因此所有三个变量都引用了同一个库。如果我是对的,你为什么要这样做?

它与import { BrowserRouter as Router, Route, Link } from 'react-router-dom' 相同吗?

4 个答案:

答案 0 :(得分:4)

声明

import {
  BrowserRouter as Router,
  Route,
  Link
} from 'react-router-dom'

BrowserRouter有选择地导入RouteLinkreact-router-domas Router语句以名称BrowserRouter(而不是Router)提供BrowserRouterRouteLink的名称不会更改。

相当于:

const Router = require("react-router-dom").BrowserRouter;
const Route = require("react-router-dom").Route;
const Link = require("react-router-dom").Link;

答案 1 :(得分:3)

不,不是那样的!它是一个添加到导入/导出功能的和声功能。

它与此相同:

let tmp = require("react-router-dom")
let Router = tmp.BrowserRouter
let Route = tmp.Route
let Link = tmp.Link

或者简单地说:

const {BrowserRouter: Router, Route, Link} = require("react-router-dom")

答案 2 :(得分:1)

import语句中的关键字unsorted[unsorted.length]允许您重命名要导入的模块。它等同于以下内容:

for (int i=1; i < unsorted.length; i++){

答案 3 :(得分:0)

导入是ES6的新语法,在您的示例中,它从模块'react-router-dom'加载三个类,BrowserRouter的名称太长,因此您可以使用别名,称为路由器。

从“module-name”导入{member1,member2 as alias2,[...]};

commonJS等效语法为:

var Router = require('react-router-dom').BrowserRouter,
    Route = require('react-router-dom').Route,
    Link = require('react-router-dom').Link;

我在下面附上了一个链接:您会找到更多详细信息。

import syntax documentation