2014-01-20 15 views
1

假設我有這樣的:如何從導入的庫中爲Dart下面導入的所有庫訪問函數/類?

benchmark.dart:

library benchmark; 
benchmark() {...} 

app.dart:

import 'benchmark.dart'; // functions from this lib are now accessible in this file 
export 'benchmark.dart'; // does this make them accessible in all files imported below? 

import 'model.dart'; 

void main() { 
    doSomething(); 
} 

model.dart

doSomething() { 
    benchmark(); // => Error, no such method, unless 
       // I import 'benchmark.dart' above in this file! 
} 

這是正確的行爲嗎?如何使基準可訪問沒有model.dart中導入該庫

回答

2

您必須在每個要使用其他庫的庫中進行導入。
您可能要做的是使用part 'model.dart';(父文件)和part of app;(鏈接的文件)將多個文件連接到一個庫。
然後,導入父文件中的類型和函數在該庫的所有文件中都可用。