prefer-template
要求使用樣板字面值而不是字串串接
在 ES2015 (ES6) 中,我們可以使用樣板字面值來取代字串串接。
const str = "Hello, " + name + "!";
const str = `Hello, ${name}!`;
規則詳情
此規則旨在標記字串中使用 +
運算子的情況。
範例
此規則的錯誤程式碼範例
在實驗場開啟
/*eslint prefer-template: "error"*/
const str = ;
const str1 = ;
此規則的正確程式碼範例
在實驗場開啟
/*eslint prefer-template: "error"*/
const str = "Hello World!";
const str1 = `Hello, ${name}!`;
const str2 = `Time: ${12 * 60 * 60 * 1000}`;
// This is reported by `no-useless-concat`.
const str4 = "Hello, " + "World!";
何時不該使用
此規則不應在 ES3/5 環境中使用。
在 ES2015 (ES6) 或更新版本中,如果您不希望收到關於字串串接的通知,您可以安全地停用此規則。
相關規則
版本
此規則在 ESLint v1.2.0 中引入。