Vue專案引用自定義外部js檔案進行使用

首先你需要了解export,在建立JavaScript模組時,export 語句用於從模組中匯出實時繫結的函式、物件或原始值,以便其他程式可以透過 import 語句使用它們。被匯出的繫結值依然可以在本地進行修改。在使用import進行匯入時,這些繫結值只能被匯入模組所讀取,但在export匯出模組中對這些繫結值進行修改,所修改的值也會實時地更新。

在資料夾A中新建一個JS檔案並命名為a。js,然後宣告一個元件b並命名為b。vue。

在a。js檔案中寫下如下內容:

let dateFormat={

date(fmt, date) {

date=new Date()

let ret;

const opt = {

“Y+”: date。getFullYear()。toString(), // 年

“m+”: (date。getMonth() + 1)。toString(), // 月

“d+”: date。getDate()。toString(), // 日

“H+”: date。getHours()。toString(), // 時

“M+”: date。getMinutes()。toString(), // 分

“S+”: date。getSeconds()。toString() // 秒

// 有其他格式化字元需求可以繼續新增,必須轉化成字串

};

for (let k in opt) {

ret = new RegExp(“(” + k + “)”)。exec(fmt);

if (ret) {

fmt = fmt。replace(ret[1], (ret[1]。length == 1) ? (opt[k]) : (opt[k]。padStart(ret[1]。length, “0”)))

}

}

return fmt;

}

}

export default dateFormat;

2。、然後在b。vue元件中透過import引用元件然後透過引用名稱加方法名獲取對應資料資訊。

原始碼示例:

此時便可獲取結果如下

Vue專案引用自定義外部js檔案進行使用

更多知識可以關注我哦,也可以前往euiamdin。com獲取原始碼,進行檢視。