Angular项目在Local主机上工作,但在GitHub页面上不工作

发布于 2025-02-13 20:53:37 字数 1018 浏览 2 评论 0 原文

我正在Angular中列出一个待办事项清单,还可以节省和还原刷新,然后在Local主机上添加到列表中 并且该项目在“添加”按钮下方显示。在github页面上,当我单击时,添加它无济于事。

github repo

github页面

这是我从控制台中获得的错误:

ERROR TypeError: Cannot read properties of null (reading 'push')
    at e.addTodo (main.2beb7ccacdd609b1.js:1:139904)
    at main.2beb7ccacdd609b1.js:1:140617
    at Qh (main.2beb7ccacdd609b1.js:1:60440)
    at i (main.2beb7ccacdd609b1.js:1:60625)
    at HTMLFormElement.<anonymous> (main.2beb7ccacdd609b1.js:1:107043)
    at v.invokeTask (polyfills.a66b249bad4eb06b.js:1:7168)
    at Object.onInvokeTask (main.2beb7ccacdd609b1.js:1:82284)
    at v.invokeTask (polyfills.a66b249bad4eb06b.js:1:7089)
    at M.runTask (polyfills.a66b249bad4eb06b.js:1:2563)
    at m.invokeTask [as invoke] (polyfills.a66b249bad4eb06b.js:1:8219)

I am making a todo list in Angular that also saves and restores on refresh, and on localhost I can add to the list
and the item displays below the add button. On GitHub Pages, when I click add it does nothing.

GitHub Repo

GitHub Pages

This is the error I am getting from the console:

ERROR TypeError: Cannot read properties of null (reading 'push')
    at e.addTodo (main.2beb7ccacdd609b1.js:1:139904)
    at main.2beb7ccacdd609b1.js:1:140617
    at Qh (main.2beb7ccacdd609b1.js:1:60440)
    at i (main.2beb7ccacdd609b1.js:1:60625)
    at HTMLFormElement.<anonymous> (main.2beb7ccacdd609b1.js:1:107043)
    at v.invokeTask (polyfills.a66b249bad4eb06b.js:1:7168)
    at Object.onInvokeTask (main.2beb7ccacdd609b1.js:1:82284)
    at v.invokeTask (polyfills.a66b249bad4eb06b.js:1:7089)
    at M.runTask (polyfills.a66b249bad4eb06b.js:1:2563)
    at m.invokeTask [as invoke] (polyfills.a66b249bad4eb06b.js:1:8219)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无力看清 2025-02-20 20:53:37

问题问题

不是来自GitHub页面。它来自角度代码本身。

该代码除了一件事外,该代码运行良好。在浏览器中首次访问Angular应用程序时, todos 键从未在该域中的 localstorage 中设置为(github页面或localhost)。因此, getTodos()函数返回null,从而使 this.todos ngoninit()中初始化语句要设置为null。

解决问题的解决方案

,将if-null Operator( ?? )添加到 getTodos()的返回中,以包含一个空数组( [] )。这样,如果是首次访问,或者用户播放并清除了 localstorage ,则该应用程序仍应工作(鉴于将新的Todos推到空数组中)。

因此,更改 todos.component.ts

return JSON.parse(localStorage.getItem("todos")!);

to

return JSON.parse(localStorage.getItem("todos")!) ?? [];

The Problem

The problem is not from GitHub Pages. It is from the Angular code itself.

The code works well except for one thing. On a first-time visit of the Angular application in a browser, the todos key has never been set before in localStorage in that domain (GitHub Pages or localhost). So the getTodos() function returns null thus making the this.todos initializing statement in ngOnInit() to be set to null.

Solution

To solve the problem, add the if-null operator (??) to the return of getTodos() to include an empty array ([]). That way, if it is a first-time visit, or if the user played around and cleared localStorage, the app should still work (given that new todos will be pushed to an empty array).

So change line 59 of todos.component.ts from

return JSON.parse(localStorage.getItem("todos")!);

to

return JSON.parse(localStorage.getItem("todos")!) ?? [];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文