"show" 和有什么不一样?和“作为"在导入语句中? [英] What is the difference between "show" and "as" in an import statement?
问题描述
import 语句中的 show
和 as
有什么区别?
What is the difference between show
and as
in an import statement?
比如有什么区别
import 'dart:convert' show JSON;
和
import 'package:google_maps/google_maps.dart' as GoogleMap;
什么时候用show
,什么时候用as
?
When do I use show
and when should I use as
?
如果我切换到 show GoogleMap
,所有对 GoogleMap
(例如 GoogleMap.LatLng
)对象的引用都被报告为未定义.
If I switch to show GoogleMap
all references to GoogleMap
(e.g. GoogleMap.LatLng
) objects are reported as undefined.
推荐答案
as
和 show
是两个不同的概念.
as
and show
are two different concepts.
使用 as
为导入的库命名.如果一个库有很多全局函数,通常这样做是为了防止它污染你的命名空间.如果您使用 as
,您可以按照您在示例中的方式访问所述库的所有函数和类:GoogleMap.LatLng
.
With as
you are giving the imported library a name. It's usually done to prevent a library from polluting your namespace if it has a lot of global functions. If you use as
you can access all functions and classes of said library by accessing them the way you did in your example: GoogleMap.LatLng
.
使用 show
(和 hide
),您可以选择要在应用程序中显示的特定类.对于您的示例,它将是:
With show
(and hide
) you can pick specific classes you want to be visible in your application. For your example it would be:
import 'package:google_maps/google_maps.dart' show LatLng;
有了这个,您将能够访问LatLng
,但不能访问该库中的其他内容.与此相反的是:
With this you would be able to access LatLng
but nothing else from that library. The opposite of this is:
import 'package:google_maps/google_maps.dart' hide LatLng;
有了这个,您将能够访问该库中除LatLng
之外的所有内容.
With this you would be able to access everything from that library except for LatLng
.
如果你想使用多个同名的类,你需要使用as
.您也可以结合使用这两种方法:
If you want to use multiple classes with the same name you'd need to use as
. You also can combine both approaches:
import 'package:google_maps/google_maps.dart' as GoogleMap show LatLng;
这篇关于"show" 和有什么不一样?和“作为"在导入语句中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!