ts-framework-file-access-rules.md 1.6 KB
Newer Older
G
ge-yafang 已提交
1 2
# Rules for Accessing Application Code Files

Z
zengyawen 已提交
3 4 5 6

The application code files can be accessed in the following ways:


G
ge-yafang 已提交
7 8 9 10 11
- Use a relative path to reference the code file. For example, if the upper-level directory is ../common/utils/utils.ets, use ./common/utils/utils.ets for the current directory.

- Use the root path of the current module to reference the code file, for example, common/utils/utils.ets.

- Store common code files in the common directory.
Z
zengyawen 已提交
12

G
ge-yafang 已提交
13 14 15 16

## Example

  
Z
zengyawen 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
```
import { FoodData, FoodList } from "../common/utils/utils.ets";

@Entry
@Component
struct FoodCategoryList {  
  private foodItems: FoodData[] = [    
    new FoodData("Tomato"),    
    new FoodData("Strawberry"),    
    new FoodData("Cucumber")  
  ]  
  build() {    
    Column() {      
      FoodList({ foodItems: this.foodItems })    
    }  
  }
}
```

Example for importing a code file:

G
ge-yafang 已提交
38
  
Z
zengyawen 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
```
//common/utils/utils.ets

export class FoodData {  
  name: string;  
  constructor(name: string) {    
    this.name = name;  
  }
}

@Component
export struct FoodList {  
  private foodItems: FoodData[]

  build() {    
    Column() {      
      Flex({justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center}) {        
        Text('Food List')          
          .fontSize(20)      
      }      
      .width(200)      
      .height(56)      
      .backgroundColor('#FFf1f3f5')      
      List() {        
        ForEach(this.foodItems, item => {          
          ListItem() {            
            Text(item.name)              
              .fontSize(14)          
          }        
        }, item => item.toString())      
      }    
    }  
  }
}
```