How do I include JavaScript packages I install from Nuget?

前端 未结 2 1171
小鲜肉
小鲜肉 2020-12-16 11:59

Possibly a stupid question. I installed Chart.js using package manager. It\'s in Solution explorer.

But where are the actual JS files or how do I get them?

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-16 12:46

    The usage of NuGet for css/javascript libraries is discouraged. For ASP.NET Core you should use the java script / node package managers, bower and npm respectively.

    You can use either one. Bower is more focused on browser libraries and css, while NPM is more for server-sided stuff (using node.js). But node.js also contains most (if not all) of the packages bower has, so it's matter of preference.# For that, you need to select your MVC project and add a new file to the project root. While in the template manager (Add->New File...), search for "Bower Configuration File" or "npm Configuration file".

    Then edit the file and add your dependency, i.e.

    package.json (npm)

    {
        "dependencies:" {
            "chart.js": "2.5.0"
        }
    }
    

    Once you save, the file will be downloaded in a directory named "node_modules`. This alone won't be enough, as the required files need to be copied over to wwwroot folder, where they can be accessed when the application runs.

    For this you'd need either use the bundler to bundle the files together (should be in default ASP.NET Core project template) or use task runners such as Gulp or Grunt to run tasks on build/publishing, which does that for you. See ASP.NET Core Docs on Gulp examples.

    Update

    Bower been deprecated now for over a year.

提交回复
热议问题