如何在Angular 2应用程序中使用fast-json-patch?

时间:2016-06-19 04:54:56

标签: json angular json-patch

我想使用" fast-json-patch" Angular 2应用程序中的库(https://github.com/Starcounter-Jack/JSON-Patch)。

我尝试过添加:

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

<{1}}下的system-config.ts文件中的

,但在导入map

时它不起作用

2 个答案:

答案 0 :(得分:4)

1)安装软件包

npm install fast-json-patch --save

2)在要使用ist的组件中,导入所需的功能:

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

3)要创建补丁,请将旧对象与新对象进行比较:

const patch = compare(oldObj, modifiedObj);

4)打印结果:

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"}

答案 1 :(得分:-2)

这是我的system-config.ts文件的样子:

    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');
    });

请添加您的代码,以便我看一下?