由于 eslint 错误而分割字符串

ntj*_*j34 1 javascript eslint

我有这个字符串:

`${this.new_post.type_to_send}-${this.new_post.france_service}-${this.new_post.service_web}`
Run Code Online (Sandbox Code Playgroud)

我收到 eslint 错误exceeds the maximum line length of...

我想把这个字符串分成几行。

谢谢你!

epa*_*llo 5

您可以仅将换行符与模板文字一起使用,但这些换行符将显示在您的字符串中。因此将其拆分为多行并使用字符串连接。

const str = `${this.new_post.type_to_send}-` + 
            `${this.new_post.france_service}-` +
            `${this.new_post.service_web}`
Run Code Online (Sandbox Code Playgroud)

或使用带有 join 的数组

const str = [this.new_post.type_to_send, 
            this.new_post.france_service,
            this.new_post.service_web].join('-')
Run Code Online (Sandbox Code Playgroud)

或者,如果行长度不太短,请使用变量来消除重复的嵌套代码。

const p = this.new_post
const str = `${p.type_to_send}-${p.france_service}-${p.service_web}`
Run Code Online (Sandbox Code Playgroud)