javascript - {`${props.title} ` } 和 {props.title} 之间的区别

标签 javascript reactjs template-strings

我在某些地方看到,为了从 props 中获取 title 的值,我们使用

{`${props.title} `}

而在其他一些地方,我们使用它

{props.title}

有什么区别?

最佳答案

第一个选项是使用模板文字,进一步阅读 here来自文档:

Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them.

第二个选项只是将字符串值传递给属性。

考虑以下因素:

const data = {
  title: 'random title'
};

console.log(data.title);
console.log(`${data.title}`);

希望对您有所帮助!

关于javascript - {`${props.title} ` } 和 {props.title} 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59812833/

相关文章:

javascript - 创建函数对象时奇怪的模板字符串行为

typescript - Twin.Macro(样式化组件)+ NextJS 中的 Typescript : Argument of type 'Interpolation<never>' is not assignable to parameter of type

javascript - 多个子元素不将状态传递回上下文(只有最后一个元素这样做)

javascript - 根据另一个对象数组过滤一个对象数组

javascript - react 路由器,redux, Electron : router isn't rendering

javascript - native react : render function not being called after state change

javascript - 在 React Web 应用程序中将 json 格式的剩余输出从蛇案例更改为 Camel 案例

javascript - 模板字符串作为对象属性名称

javascript - for循环和 promise

javascript - 从 vue.js 中的 foreach 循环访问变量