3
如何通過打字稿在angular2應用程序中獲得關鍵值。如何閱讀打字稿中的web.config鍵
add key="localPath" value="http://localhost:618/"
add key="serverPath" value="http://api.azure.net/"
我想在.ts文件中得到這些「localpath」和「serverpath」的值。
請給我建議。
如何通過打字稿在angular2應用程序中獲得關鍵值。如何閱讀打字稿中的web.config鍵
add key="localPath" value="http://localhost:618/"
add key="serverPath" value="http://api.azure.net/"
我想在.ts文件中得到這些「localpath」和「serverpath」的值。
請給我建議。
由於TypeScript被轉換爲JavaScript,因此無法直接從配置文件中讀取。
如果要動態更改服務的URL(比如從DEV端點到QA或生產端點),您可以使用剃刀語法在你的HTML/CSHTML文件抓取從配置文件中的值,然後將的值注入腳本中,或可以在部署過程中使用Powershell替換構建過程中TypeScript文件中的值。
# Argument passed into PowerShell script (or write out URL string manually).
$NewURL1 = $args[0];
$NewURL2 = "http://goodURL.com/api";
# *.js files, because your *.ts files will already be transpiled.
$files = gci "C:\MyProject\DropFolder" -recurse -include "exampleWithBadURL.js", "otherFile.js";
Foreach ($file in $files)
{
$fileContent = Get-Content($file) -Raw;
attrib $file -r;
# Perform the replacement.
$fileContent -replace "http://localhost:38774/", $NewURL1 | Out-File $file;
$fileContent -replace "http://badURL.com/api/", $NewURL2 | Out-File $file;
Write-Output($file.FullName);
}
如果您的構建/脫模劑允許的話,你可以部署你的代碼到一個特定的環境中,當運行這個PowerShell腳本。我在中使用了一個類似的腳本,在我發佈的定義中爲VSTS/TFS我複製了這些文件。