我有一個(gè)任務(wù),我是 angular 的新手。我必須將來自提交表單的數(shù)據(jù)推送到我在本地?fù)碛械默F(xiàn)有 json 文件中。我的任務(wù)是創(chuàng)建一個(gè) angular 應(yīng)用程序,用戶可以在其中創(chuàng)建新任務(wù),然后在不同的頁面上查看它們。我嘗試使用 http.post,但沒有成功,然后出于某種原因,我認(rèn)為推送可能會起作用,因?yàn)?json 是一個(gè)數(shù)組,但這并沒有真正起作用,因?yàn)槲矣悬c(diǎn)無能為力,哈哈。這是我的 json 文件的樣子,要求還說明我必須有一個(gè)硬編碼,所以它是這樣的:{ "tasks": [ { "name": "Dishes", "timeToComplete": "10 Minutes", "description": "Cleaning the dishes", "itemsNeeded": "Soap, Sponge, Water, Dirty Dishes!" } ]}這是具有表單的組件的 html 代碼: <div> <h3>Create A New Task</h3> <form (ngSubmit)='onSubmit()'> <label for='name'>Name of Task:</label> <input type='text' id='name' placeholder='Name' name='name' required [(ngModel)]='task.name' /> <br> <label for='time'>Estimated Time to Complete:</label> <input type='text' id='time' placeholder='Time' name='timeToComplete' required [(ngModel)]='task.timeToComplete' /> <br> <label for='description'>Description of Task:</label> <input type='text' id='description' placeholder='Description' name='description' required [(ngModel)]='task.description' /> <br> <label for='itemsNeeded'>Items to complete task:</label> <textarea type='text' id='itemsNeeded' placeholder='Items' name='itemsNeeded' required [(ngModel)]='task.itemsNeeded'></textarea> <button type='submit'>Submit</button> <p>If all looks good, go ahead and hit submit!</p> </form> <hr> <div> <h1>Here's what your task looks like: </h1> <h3>Your Task: {{ task.name }}</h3> <h4>Time it will take: </h4><p>{{ task.timeToComplete }}</p> <h4>Description of what you will be doing: </h4><p>{{ task.description }}</p> <h4>What you will need, to succeed! </h4><p>{{ task.itemsNeeded }}</p> </div></div>您在下面看到的代碼只是為了查看輸入是否正確接線。最后這里是我的組件的 .ts 文件,包括 onSubmit 函數(shù)控制臺工作并顯示從提交返回的對象,所以我知道這是有效的,但是我將如何將它添加到上面的 json 文件中?
如何將提交的數(shù)據(jù)推送到現(xiàn)有的本地 json 文件中?
慕的地6264312
2021-09-30 16:59:33