Angular2 load script conditionally using Angular CLI?

前端 未结 2 665
南旧
南旧 2021-01-13 09:44

Can I load scripts conditionally using Angular CLI???

In this file, I want to load one script depending on the enviroment or a variable. So In production I want to l

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-13 10:18

    The .angular-cli.json supports configuration for multiple apps. Therefore we can put completely different configuration with different scripts/styles for the same app.

    "apps": [
        {
          "styles": [
            "styles.css"
          ],
          "scripts": [ ],
          "environmentSource": "environments/environment.ts",
          "environments": {
            "dev": "environments/environment.ts",
            "prod": "environments/environment.prod.ts",
            "local": "environments/environment.local.ts"
          }
        },
        {
          "styles": [
            "styles.css",
            "../node_modules/select2/dist/css/select2.min.css"
          ],
          "scripts": [
            "../node_modules/jquery/dist/jquery.min.js",
            "../node_modules/select2/dist/js/select2.full.min.js"
          ],
          "environmentSource": "environments/environment.ts",
          "environments": {
            "dev": "environments/environment.ts",
            "prod": "environments/environment.prod.ts",
            "local": "environments/environment.local.ts"
          }
        }
    

    To serve the default app: $ ng serve --app=0

    or

    $ ng serve --app 0
    

    To serve the app with local environment:

    $ ng serve --app=1 --env=local --port=8091
    

提交回复
热议问题