[英]Specifying the configuration to be used when running an Aurelia application
我使用 Aurelia CLI 中的au new
命令創建了一個默認的 Aurelia Typescript 應用程序。
在項目的根目錄下,可以找到一個config
目錄,其中包含environment.json
和environment.production.json
文件。 我的看起來像這樣:
環境.json
{
"debug": true,
"testing": true,
"stringVal": "Hello World"
}
環境.生產.json
{
"debug": false,
"testing": false,
"stringVal": "Hello Production"
}
從命令行運行時,我希望能夠在不同的運行中使用不同的配置。 我目前的app.ts和app.html如下:
應用程序.ts
import environment from '../config/environment.json';
export class App {
public message = environment.stringVal;
}
應用程序.html
<template>
<h1>${message}</h1>
</template>
為了更好地衡量,我的 main.ts 文件如下:
import {Aurelia} from 'aurelia-framework';
import environment from '../config/environment.json';
import {PLATFORM} from 'aurelia-pal';
export function configure(aurelia: Aurelia): void {
aurelia.use
.standardConfiguration()
.feature(PLATFORM.moduleName('resources/index'));
aurelia.use.developmentLogging(environment.debug ? 'debug' : 'warn');
if (environment.testing) {
aurelia.use.plugin(PLATFORM.moduleName('aurelia-testing'));
}
aurelia.start().then(() => aurelia.setRoot(PLATFORM.moduleName('app')));
}
au run
(或npm start
)以指定要使用的配置?根據您的代碼,我相信#1 的問題應該已經通過傳遞給您的運行 function 的正確環境參數來解決。 我的一個建議是制作一個environment.ts
文件來為你處理這個問題。 例如。
環境.ts
import env from '../config/environment.json';
function stringVal() {
return env.stringVal;
}
export { stringVal }
然后在你的 app.ts 你可以做
import { stringVal } from './environment';
export class App {
public message = stringVal();
}
對於#2的答案,它已在您的問題的評論中提到,但我也會在這里分享。 --env
是你要找的這里是我用於我的 Au1 項目的腳本
"scripts": {
"analyze": "webpack --env production --analyze",
"build": "webpack --env production",
"build:dev": "webpack",
"build:staging": "webpack --env staging",
"start": "webpack-dev-server"
},
我的config
文件夾中有一個environment.staging.json
, environment.json
, environment.production.json
,它們都按預期工作。
希望這可以幫助! 祝你好運
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.