Add .js scripts in Angular 2 project

前端 未结 3 652
慢半拍i
慢半拍i 2021-01-04 09:22

I am building a web app with Angular 2 framework and I want to use an external template (https://freehtml5.co/preview/?item=splash-free-html5-bootstrap-template-for-any-webs

相关标签:
3条回答
  • 2021-01-04 09:43

    If you're using the Angular CLI you can include .js files from your node_modules in your .angular-cli.json.

    Right under the the styles array and above the environmentSource you should see the scripts array. I've included a little bit (I've truncated it) of the JSON as reference for you.

    {
      "apps": [
        {
          "root": "src",
          "outDir": "dist",
          "assets": [
            "assets",
            "favicon.ico"
          ],
          "index": "index.html",
          "main": "main.ts",
          "styles": [
            "styles.scss"
          ],
          "scripts": [
            "../node_modules/hammerjs/hammer.min.js"
          ],
          "environmentSource": "environments/environment.ts",
          "environments": {
            "dev": "environments/environment.ts",
            "prod": "environments/environment.prod.ts"
          }
        }
      ]
    }
    
    0 讨论(0)
  • 2021-01-04 09:55

    i think loading script tags inside angular 2 components is not allowed but take a look at this it may help you How do I include a JavaScript file in another JavaScript file?

    0 讨论(0)
  • 2021-01-04 09:59

    Script tags are not allowed in component templates. Including scripts in your index.html should make them globally accessible. It isn't clear what you mean when you say you're browsing to another page, but if you use the angular router with router-outlet all scripts you import should be accessible.

    0 讨论(0)
提交回复
热议问题