小编pla*_*ter的帖子

如何在 Angular 8 中使用服务实现行为主体

我是 Angular 的新手,但遇到了问题。

我正在创建一个包含多个兄弟组件的应用程序。当我更新一个组件中的值时,其他组件不会更新。我知道要解决这个问题,我应该使用行为主题。但是我如何用我的服务、组件和所有模板来实现它?

这是我的代码 -

---------------我的服务-------------------------- ——

//import


@Injectable() 
export class CoachService {

    apiURL = environment.apiURL;

    constructor(private http: HttpClient ) { }

    coachProfile(token :string)
    {  
    return this.http.post<any>(this.apiURL+'/coach/profile_infos',{
      token: token
      })        
    }

    updateProfile(info: any, token: string, us_id: string) {
      return this.http.post<any[]>(this.apiURL + '/coach/update_profile', {
        token: token,
        us_id: us_id,
        us_lang: info.us_lang,
        us_firstname: info.us_firstname,
        us_lastname: info.us_lastname,
        us_sex: info.us_sex,
        us_birthdate: info.us_birthdate,
        us_national_number : info.us_national_number,
        us_email: info.us_email,
        us_gsm: info.us_gsm,        
        online_profile: info.online_profile,          
        us_address: info.us_address,
        us_zip: info.us_zip,
        us_city: info.us_city,
        country:{
          id: info.country.id …
Run Code Online (Sandbox Code Playgroud)

httpclient typescript behaviorsubject angular angular8

19
推荐指数
2
解决办法
4万
查看次数

从自定义路径预加载 env 文件

我试图在节点应用程序启动时预加载 .env 文件。

我有以下配置,但每当我运行时npm run start:dev都会出现错误。

"scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "prestart": "npm run build",
    "start": "node .",
    "start:dev": "node -r dotenv/config . dotenv_config_path=/.dev.env",
    "start:test": "node -r dotenv/config . dotenv_config_path=/.test.env",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
Run Code Online (Sandbox Code Playgroud)

或者

"scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "prestart": "npm run build",
    "start": "node .",
    "start:dev": "node -r dotenv/config . dotenv_config_path=./.dev.env",
    "start:test": "node -r dotenv/config . dotenv_config_path=./.test.env",
    "test": "echo \"Error: no test specified\" && …
Run Code Online (Sandbox Code Playgroud)

node.js dotenv

8
推荐指数
1
解决办法
1万
查看次数

NodeJS Sharp 节点包内存消耗问题

我正在开发一个用于sharp node package调整 JPEG/JPG 图像大小的 NodeJS 项目。

问题是Node进程不断地将处理后的文件添加到内存中并且从不释放它。

因此,消耗的内存量随着每个请求而增加,并且永远不会被释放。

调试应用程序后,我意识到Sharp toBuffer API导致了该问题。

我尝试通过创建 acustom writable stream并用管道来使用替代解决方案sharp Duplex stream,但最终遇到了同样的问题。

我不明白我是否在这里遗漏了任何东西或者这是一个错误。

分享下面的代码(我删除了不需要的代码以使其紧凑)-

const { Writable } = require("stream");
const { createServer } = require("http");
const { readFileSync } = require("fs");
const sharp = require("sharp");

async function resizeJpeg(input_buffer) {
    // initialise file, the response object
    const file = { normalisedImage: null, originalImage: null };
    // initialise sharp instance using original image buffer
    let image = …
Run Code Online (Sandbox Code Playgroud)

memory-leaks image-processing node.js sharp

7
推荐指数
1
解决办法
2789
查看次数

NodeJS 内存消耗统计问题 - 巨大的 RSS 和外部内存使用量

在为 Node 项目进行性能测试时,我遇到了一些内存报告,这些报告在性能和内存泄漏方面的解释有点令人困惑。

背景

因此,我使用process.memoryUsage() API生成了报告,该 API 提供了有关以下信息的信息 -

  • RSS - 总分配内存的子集,包括堆、代码段和堆栈
  • heapTotal - 分配给对象、字符串和闭包的内存
  • heapUsed - 对象、字符串和闭包使用的实际内存
  • external - 绑定到 V8 管理的 JavaScript 对象的 C++ 对象的内存使用

问题

我在每次向服务器发出请求后记录了内存快照,并绘制了 100 个请求的图表 -

图表显示以下信息 -

  1. RSS内存随着服务的每个请求而不断增加
  2. External Memory相比用法是显著Heap Memory
  3. External Memory一旦进程空闲,它就会关闭
  4. Total HeapUsed Heap受到控制的记忆是很好

发现

根据我的理解,Heap 之外的某些东西导致内存上升。

我正在使用sharp node package它调整大量大图像的大小,最终为这些图像生成巨大的内存缓冲区。

在内存方面,每个请求都提供 10 张图像,最终生成 80 MB 的缓冲区数据

这是我遇到这个 github 问题的地方,讨论了在 NodeJs 中创建缓冲区的影响

这个答案在同一个 github 问题中,解释了缓冲区如何可能占用garbage collector …

buffer garbage-collection memory-leaks node.js sharp

7
推荐指数
0
解决办法
1038
查看次数

为什么即使在异步功能中使用时,等待也无法正常工作

我做了一个通过电子邮件ID搜索用户的功能。我正在使用await在异步函数中调用该函数,并将返回值分配给或常量/变量,但在打印常量/变量时变得不确定

function search(email) {
    sql = `SELECT email FROM users WHERE email = '${email}'`;
    db.query(sql, (err, res) => {
        if (err) {
            console.log(err);
        }
        else {
            return res[0].email;
        }
    })
}

const auth = async (req, res, next) => {
    try {
        const token = req.header('Authorization').replace('Bearer', '');
        const decoded = jwt.verify(token, 'catisdoguniversaltruth');
        const user = await search(decoded._id);
        console.log(user);
        if (!user) {
            throw new Error();
        }
        next();
    }
    catch (e) {
        res.status(401).send("Not Authenticated, Please login");
    }
};

module.exports = auth;
Run Code Online (Sandbox Code Playgroud)

javascript node.js promise

0
推荐指数
1
解决办法
112
查看次数