iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 4
1
自我挑戰組

從零開始的後端異世界生活系列 第 4

Day 04 - 開啟方便的引入相對路徑設定

今天是個忙碌的星期六,我們就先來寫點簡單但實用的東西 /images/emoticon/emoticon37.gif

我的路徑不是你的路徑

在開發專案的時候,常常會需要引入(import)另一個檔案,取用它其中的物件或方法,比方說:

我的 TaskController 需要引入 TaskService,想要取用 Service 裡面撰寫的方法

https://ithelp.ithome.com.tw/upload/images/20200919/201196190OqHiMxox6.png

在沒有修改設定前,引入的通常會是絕對路徑,大概會長這樣:

https://ithelp.ithome.com.tw/upload/images/20200919/20119619ofkn0kIeJO.png

這時候在本地端跑測試時(yarn test)可能沒問題,但是你推到遠端上跑 CI 時可能會發生以下錯誤:

https://ithelp.ithome.com.tw/upload/images/20200919/20119619lIyfyhtxrg.png
(範例圖)

它不認識你的絕對路徑。

開啟方便的設定

可以在左上角的導航列打開設定欄:
https://ithelp.ithome.com.tw/upload/images/20200919/20119619nXf0iBseyy.png

或者按下 Command+Shift+P 後輸入 Settings 打開 Settings UI:
https://ithelp.ithome.com.tw/upload/images/20200919/20119619VwbkD8je6N.png

開啟後搜尋輸入 preferences,找到以下地方將它改為 relative:
https://ithelp.ithome.com.tw/upload/images/20200919/20119619fCM6pkRmqe.png

若是使用 JavaScript 開發則是在這:
https://ithelp.ithome.com.tw/upload/images/20200919/20119619XSSx56fbdy.png

之後重開 VSCode 再來試試看:
https://ithelp.ithome.com.tw/upload/images/20200919/201196196yMOArvEj8.png

Yes,可以順利引入相對路徑了。

/*
本系列文係個人新手開發心得,可能會有許多錯誤,煩請多多包容不吝指教。
*/


上一篇
Day 03 - 建立你的第一個 NestJS 專案與熟悉 Nest CLI
下一篇
Day 05 - 簡易 NestJS 專案導覽(上) 與引入 Dependencies
系列文
從零開始的後端異世界生活30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言