Home > Web Front-end > JS Tutorial > body text

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

青灯夜游
Release: 2023-03-03 19:52:45
forward
2196 people have browsed it

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/"
    }
  ]
})
Copy after login

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"
    }
  }
}
Copy after login

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>
Copy after login

We want to <base href="/"> becomes <base href="/jimmy/">. 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/"
}
Copy after login

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;
  }
}
Copy after login
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!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template