angular - 如何将配置传递到 angular-cli 中的 indexTransform 中?

标签 angular angular-cli angular-cli-v8

我想在 Angular 8 (Angular-cli) 项目中使用 indexTransform 功能,但 targetOptions 的配置属性仍为空。

我在这里尝试了示例项目:https://github.com/just-jeb/angular-builders/tree/master/packages/custom-webpack/examples/full-cycle-app

但在示例项目中,配置也是空的。

module.exports = (targetOptions, indexHtml) => {
    targetOptions.configuration // configuration is empty
}

在哪里可以定义应传递到回调函数中的配置?

最佳答案

配置反射(reflect)了在 ng build 中传递给 --configuration=configurationName 选项的值,因此在开发模式下始终为空。

如果您使用 --prod 标志(即 --configuration=product 的别名)进行构建,您可以看到配置已正确填充

下面您可以找到一个使用示例。此代码在 head 元素中添加一个带有 api 服务位置的元标记元素。

const allSettings = {
    development: {
        apiUrl: "http://localhost:8000"
    },
    production: {
        apiUrl: "https://myapiserver/"
    }
}

module.exports = (targetOptions, indexHtml) => {
    const i = indexHtml.indexOf('</head>');

    // if empty assumes 'development'
    const configuration = targetOptions.configuration || "development";

    // load settings (by configuration)    
    const settings = allSettings[configuration];

    // build meta tag
    let customHead = "";
    customHead += `<meta name="apiUrl" content="${setting.apiUrl}">`;

    // add customHead before head end tag
    return `${indexHtml.slice(0,i)}
            ${customHead}
            ${indexHtml.slice(i)}`
}

关于angular - 如何将配置传递到 angular-cli 中的 indexTransform 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57419530/

相关文章:

angular - 运行 Angular e2e 测试时如何禁用或绕过 MSAL 身份验证?

Angular cli 显示错误 "An unhandled exception occurred: No projects support the ' test' target。” 运行 ng test 或 ng serve 命令时

Angular 客户端和 webpack

Angular 延迟加载 : route to a specific feature component

angular - 如何使用 Angular 服务使用所有 api 请求通用的 header ?

angular - 在 Angular 库中保留符号链接(symbolic link)

node.js - Angular 8 - GET styles.js net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK) 当通过 USB 服务时

javascript - 实时什么时候我们会用到AOT和JIT

javascript - ng build --prod 给我一个错误

angularjs - Node Js Angular cli,ng 服务错误,套接字关闭