Angular – 如何区分用户的生产环境和开发环境

huangapple go评论80阅读模式
英文:

Angular - how to tell the difference between prod and dev for user

问题

我正在进行一个Angular项目,我有两个单独的链接,一个用于开发环境,一个用于生产环境,用于测试不同的功能,但我想要能够在一个环境中更改一些CSS,以便测试的用户可以知道他使用了哪个链接,以防误点击了另一个链接。如果有更简单的方法来实现相同的功能,但不需要更改CSS,那也可以,我只需要在GUI中显示一些内容。

我尝试了制作两个CSS文件,然后在angular.json中为每个配置添加它们,就像这样,但这没有起作用:

"stylePreprocessorOptions": {
    "includePaths": [
        "src/styledev.css"
    ]
}
英文:

Im working on an angular project and I have two separate links, one for dev and one prod, for testing different functionalities but I want to be able to change some css for one environment so the user who's testing can know what link he used if he miss clicked the other one. If there is an easier method to do the same thing but you dont need to change css is ok, i just need to show something in gui.

I tried making two css files and then i added them on angular.json for each configuration like this but this didnt work

"stylePreprocessorOptions": {
          "includePaths": [
            "src/styledev.css"
          ]
        }

答案1

得分: 2

创建不同的环境文件是一个更好的方法

例如创建environments/dev.ts

export const environment = {
  production: false,
  devMode: true,
  css: [
    './styles/dev.css',
  ],
};

另一个用于生产的如environment/prod.ts

export const environment = {
  production: true,
  devMode: false,
  css: [
    './styles/prod.css',
  ],
};

然后你可以使用environment变量来加载相应的CSS文件。

方法1:(首选方式)
使用ng serve命令并带上--configuration标志。

这个标志允许你指定在服务应用程序时要使用的环境。

方法2:(尚未测试,但根据我的理解应该可以工作)

app.component.ts中:

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [
    environment.css,
  ],
})
英文:

Creation of different environment file is a better approach

for e.g creating environments/dev.ts

export const environment = {
  production: false,
  devMode: true,
  css: [
    './styles/dev.css',
  ],
};

and one for production like environment/prod.ts

export const environment = {
  production: true,
  devMode: false,
  css: [
    './styles/prod.css',
  ],
};

Then you can use the environment variable to load the appropriate CSS file.

Method 1: (Prefered way)
use the ng serve command with the --configuration flag.

This flag allows you to specify the environment that you want to use when serving your application.

Method 2: (have not tested it but as per my understanding it should work)

In app.component.ts:

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [
    environment.css,
  ],
})

答案2

得分: 1

我使用一个变量来解决了这个问题,我将它分配给了 isDevMode(),然后在 HTML 文件中使用了这个变量:

<mat-toolbar *ngIf="dev === true" style="background-color: red;" color="primary">
</mat-toolbar>

<mat-toolbar *ngIf="dev === false" color="primary">
</mat-toolbar>

现在用户可以知道在哪个环境中进行测试。

英文:

I resolved the problem using a variable that i assigned the isDevMode() then in the html file i used this

&lt;mat-toolbar *ngIf=&quot;dev === true&quot; style=&quot;background-color: red;&quot; color=&quot;primary&quot;&gt;
&lt;/mat-toolbar&gt;

&lt;mat-toolbar *ngIf=&quot;dev === false&quot; color=&quot;primary&quot;&gt;
&lt;/mat-toolbar&gt;

now the user can know in which environment is testing

huangapple
  • 本文由 发表于 2023年5月24日 17:20:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76321950.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定