Home >Web Front-end >JS Tutorial >Detailed explanation of how to add a specified access prefix to the path in the Angular project

Detailed explanation of how to add a specified access prefix to the path in the Angular project

青灯夜游
青灯夜游forward
2023-03-03 19:52:452227browse

How to add prefix to path in Angular project? The following article will introduce you to the method of adding a specified access prefix to the Angular project path. I hope it will be helpful to you!

Detailed explanation of how to add a specified access prefix to the path in the Angular project

When developing multiple projects, we hope to access different projects through specified prefix paths. For example, use the prefix /projectA/ to access project A; use the prefix /projectB/ to access project B. How should we set it up?

The framework used here is Angular, "@angular/core": "~12.1.0"

Change project prefix

Suppose the prefix we add is /jimmy/

1. Change routing prefix

Add APP_BASE_HREF in app.module.ts file:

import { APP_BASE_HREF } from '@angular/common';

@NgModule({
  providers: [
    {
      provide: APP_BASE_HREF,
      useValue: "/jimmy/"
    }
  ]
})

2. Change the packaged file

This step is not necessary, we just unify the name as jimmy [Related tutorial recommendation: "angularTutorial》】

Change the output file of angular.json:

{
  "projects": {
    ...
    {
      "outputPath": "jimmy"
    }
  }
}

3. Change the mounting file The base href

By default, the mounted file index.html generally looks like this:

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Jimmy</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="logo.png">
</head>
<body>
  <app-root></app-root>
</body>
</html>

We want to 62ef4d59b51785055dc2c793448a1c82 becomes dc18e036a3e1529e6ddd57b180e62105. However, we cannot manually change the mount file. Because only the files after the build are changed, we can complete this step in the package.json file. Just add --base-href=/jimmy/, as follows:

"scripts": {
  "build": "ng build --base-href=/jimmy/"
}

Runnpm run build The folder obtained after packagingjimmy The base tag in the index.html file under will naturally change.

So far, we have changed the accessed project prefix, so what should we do if we want to deploy it to the server for access?

Deployment project

Assume here that I have uploaded the packaged jimmy resource to the server and used nginx as acting.

This project is a SPA project. The explanation of the MPA project will be placed in the next article. The related project technology is next.js, so stay tuned

Here, we need to changenginx.config# The server field in ##:

server {
  listen 80 default_server;
  root /usr/share/nginx/fe/; // 打包的文件存放的位置
  
  location /jimmy/ {
    index  index.html index.htm;
    try_files $uri $uri/ /jimmy/index.html;
  }
}

Execute

nginx -s reload to make the configuration take effect. Project jimmy can be accessed through http://domain.com/jimmy/index.html.

For more programming-related knowledge, please visit:

Introduction to Programming! !

The above is the detailed content of Detailed explanation of how to add a specified access prefix to the path in the Angular project. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete