What is the assets array in Angular application angular.json

In an Angular project, angular.jsonit is a very important configuration file used to define and manage various settings and build options of the project. Among them, assetsthe array is angular.jsona key configuration item in , which is used to specify the static resource files and folders that need to be included in the application after construction. In this article, I'll explain what assetsan array is and provide detailed examples of how it can be used to manage static resources in your project.

What is assetsan array?

assetsAn array is a property in an Angular project's angular.jsonconfiguration file that defines which static resource files and folders should be included in the built application. These resources can be images, font files, JSON files, style files, HTML files, etc. They usually do not need to be compiled or transformed during the build process, but are simply copied to the final output directory for use in the application .

assetsThe main functions of arrays include:

  1. Managing static resources : With assetsarrays, you can tell the Angular build tool which static resources need to be included in the final build output so that the application can use them.

  2. Maintain project structure : It allows you to maintain the file structure of your project, ensuring that the location of static resources remains the same as in the source code after a build.

  3. Simplified Deployment : By adding static resources to assetsan array, you can ensure that they are automatically copied to the correct location when the application is deployed, eliminating the need for manual work.

  4. Improve performance : Adding static resources to the application can reduce resource loading time, thereby improving the performance of the application.

assetsArray configuration example

Let's walk through a detailed example of how to configure assetsarrays to manage static resources in your project. Suppose you are developing an e-commerce website, you need to include some images, font files and JSON data files in your application.

Step 1: Openangular.json

angular.jsonFirst, open the file in your Angular project , which is usually located in the project's root directory.

Step 2: Find projectsthe Part

In angular.json, project configuration is usually located projectsunder a JSON object called , which contains various configurations for your Angular project. In this object, find your project's configuration, usually keyed by the project's name. For example:

{
    
    
  "projects": {
    
    
    "your-project-name": {
    
    
      // 项目配置将在这里
    }
  }
}

Step 3: Configure assetsthe Array

Inside the project configuration, you'll find a architectsub-object called , which contains various build and deployment configurations. In architectObjects, find your project's build configuration, usually named build, for example:

{
    
    
  "projects": {
    
    
    "your-project-name": {
    
    
      "architect": {
    
    
        "build": {
    
    
          // 构建配置将在这里
        }
      }
    }
  }
}

In the build configuration, there is a optionssub-object named , which contains various options related to the build. In optionsthe object, you can find assetsan array of configurations, which are used to specify static resource files and folders to be included in the build output.

Here is assetsthe configuration for an example array:

{
    
    
  "projects": {
    
    
    "your-project-name": {
    
    
      "architect": {
    
    
        "build": {
    
    
          "options": {
    
    
            "assets": [
              "src/assets",
              "src/favicon.ico",
              {
    
    
                "glob": "**/*",
                "input": "src/assets/images",
                "output": "assets/images"
              },
              {
    
    
                "glob": "data.json",
                "input": "src/app/data",
                "output": "assets/data"
              }
            ],
            // 其他构建选项将在这里
          }
        }
      }
    }
  }
}

In the above example, assetsthe array contains multiple configuration items, and each configuration item describes the path and output directory of a static resource. Let's explain these configuration items one by one:

  • "src/assets": This is a simple string that says to src/assetscopy the contents of the entire folder into the build output directory.

  • "src/favicon.ico": This string means to src/favicon.icocopy the file into the build output directory.

  • {"glob": "**/*", "input": "src/assets/images", "output": "assets/images"}: This configuration item uses more detailed options. globThe attribute uses wildcards **/*, which means to copy src/assets/imagesall the contents of the folder to assets/imagesthe directory. inputThe and outputattributes specify the source and destination paths, respectively.

  • {"glob": "data.json", "input": "src/app/data", "output": "assets/data"}: This configuration item src/app/data/data.jsoncopies files into assets/dataa directory.

Step 4: Build the Application

After configuring assetsthe array, you can build your Angular application by running:

ng build

This will trigger the Angular CLI build process and assetscopy the static resources configured in the array to the build output directory.

Step 5: Review the build output

assetsOnce the build is complete, you can view the static resources configured in the array in the build output directory . By default, the build output directory is located dist/your-project-nameunder the directory where your-project-nameis your project name.

By configuring assetsthe array, you can ensure that these static resources are automatically included when the application is deployed, so that the application can properly load these resources and provide the required functionality

Summarize

assetsArrays are a key configuration item in Angular projects and are used to define static resource files and folders that need to be included in the built application. Through examples, we detail how to configure assetsthe array, including how to specify the path and output directory of static resources. By using assetsarrays, you can manage static resources in your project, ensuring that they are automatically copied to the correct location during application build and deployment, improving the performance and maintainability of your application. This is an important configuration option in Angular projects, and developers should be familiar with how to use it to manage static resources.

Guess you like

Origin blog.csdn.net/i042416/article/details/132663704