Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Angular Material with VS2017 SPA Templates

I'm building an Angular2 app using the SPA templates and JavaScriptServices provided by Microsoft (https://blogs.msdn.microsoft.com/webdev/2017/02/14/building-single-page-applications-on-asp-net-core-with-javascriptservices/).

I'd like to use Angular Material as opposed to Bootstrap for styling/themeing, however I'm unable to get it to work.

Following the setup guide (https://material.angular.io/guide/getting-started) is straightforward enough, however when it comes to including the theme assets (@angular/material/prebuilt-themes/...) from the npm package in the wwwroot dist output to make them available to the app I'm at a complete loss.

I assume it's simply a case of modifying the WebPack configuration, however after an hour or two of tinkering and google searches I'm no closer to understanding what to do.

Can anybody point me in the right direction?

NB. Please don't suggest copying the files I need to the wwwroot or linking to a CDN etc.

like image 324
James Law Avatar asked Jul 13 '17 12:07

James Law


1 Answers

I think I've possibly found a more complete/comprehensive way to achieve this...

  1. Close your Visual Studio solution
  2. Open the project folder and delete the node_modules directory
  3. Open up a command prompt for the project directory
  4. Run npm install --save @angular/material @angular/cdk in command prompt
  5. Run npm install --save @angular/animations in command prompt
  6. Upgrade the @angular and rxjs packages to the latest/compatible versions in package.json. This is a headache. I have no idea which are the right versions and which are wrong!
  7. Run npm install in command prompt
  8. Update webpack.config.vendor.js, adding the following two values

    const nonTreeShakableModules = [
        ...
        '@angular/material',
        '@angular/material/prebuilt-themes/deeppurple-amber.css'
    ];
    
  9. Run webpack --config webpack.config.vendor.js in command prompt

  10. Open the solution in Visual Studio
  11. Build the solution, after which you should be able to use it, having followed the "Getting Started" guide at https://material.angular.io/guide/getting-started

I've checked in a vaguely working version to GitHub - it can be found at https://github.com/alterius/AngularMaterial2DotNetCoreSPA

like image 77
James Law Avatar answered Oct 11 '22 01:10

James Law