10

I want to use the "fast-json-patch" library (https://github.com/Starcounter-Jack/JSON-Patch) in an Angular 2 application.

I have tried adding:

'fast-json-patch': 'vendor/fast-json-patch/dist/json-patch-duplex.min.js'

in the system-config.ts file under the map but it doesn't work when importing fast-json-patch

Rocky
  • 391
  • 5
  • 14

2 Answers2

23

1) Install the package

npm install fast-json-patch --save

2) In the component, where you want to use ist, import the functions you need:

import { compare } from 'fast-json-patch';

3) To create a Patch compare the old object with the new object:

const patch = compare(oldObj, modifiedObj);

4) Print out the result:

console.log(patch);

0:{op: "replace", path: "/firmendetails/registergericht", value: "Darmstadt xx"}
1:{op: "remove", path: "/firmendetails/handelsname"}
2:{op: "remove", path: "/firmendetails/firma"}
3:{op: "remove", path: "/firmendetails/rechtsform"}
Jojo.Lechelt
  • 1,259
  • 12
  • 15
-5

This is how my system-config.ts file looks like:

    import * as express from 'express';
    import {ng2engine} from 'angular2-universal-preview';

    // Angular 2
    import {App} from './src/app';

    let app = express();

    // Express View
    app.engine('.ng2.html', ng2engine);
    app.set('views', __dirname);
    app.set('view engine', 'ng2.html');


    // static files
    app.use(express.static(__dirname));


    app.use('/', (req, res) => {
      res.render('index', { App });
    });



    app.listen(3000, () => {
      console.log('Listen on http://localhost:3000');
    });

Please add your code, so I can take a look?

null canvas
  • 10,201
  • 2
  • 15
  • 18