简体   繁体   English

如何连接两个变量并分配给闪电网络组件中闪电卡的标签属性

[英]How to concatenate two variables and assign to the label attributes of lightning-card in lightning web component

I have a lightning-card in lightning web component (LWC) and want to set the label attribute with two different variables.我在闪电网络组件 (LWC) 中有一个闪电卡,并希望使用两个不同的变量设置标签属性。 Although this can be done through the controller but I want to do this in html file itself.虽然这可以通过控制器完成,但我想在 html 文件本身中执行此操作。

As in the code snippet, I am assigning {cardTitle} as a title, but I have another variable {totalCount} and want to concatenate the totalCount along with the cardTitle here.在代码片段中,我将 {cardTitle} 指定为标题,但我还有另一个变量 {totalCount} 并希望在此处将 totalCount 与 cardTitle 连接起来。 So lightning-card should have title like "{cardTitle}{totalCount}".所以闪电卡的标题应该像“{cardTitle}{totalCount}”。

<lightning-card title={cardTitle}></lightning-card>

//In Controler js
@track cardTitle = 'Student details';
@track totalCount = 0; //This will be set by the apex controller later and will have dynamic number

When I try below code当我尝试下面的代码时

<lightning-card title={cardTitle}{totalCount}></lightning-card>

It shows error as它显示错误为

multiple expressions found找到多个表达式

. .

No. You can only do it in controller JavaScript file.不可以。您只能在 控制器 JavaScript 文件中执行此操作。

I love part of the answer on SF stackexchange so I'm going to quote it here:我喜欢SF stackexchange 上的部分答案,所以我将在这里引用它:

Your concerns about separating UI from controller logic do not apply here as this is not a "controller".您对将 UI 与控制器逻辑分离的担忧不适用于此处,因为这不是“控制器”。 That MVC pattern is an Aura-ism. MVC 模式是一种光环主义。 This is the code which drives your component's functionality so it makes sense that your JS would know about class names.这是驱动组件功能的代码,因此您的 JS 知道类名是有意义的。

It's different but think about it that way - it'll let you write a proper unit test for the JavaScript.它是不同的,但要这样想——它会让你为 JavaScript 编写一个适当的单元测试。 How you'd test logic that exists only in HTML layer?您将如何测试仅存在于 HTML 层中的逻辑? Or only in Visualforce page markup?还是仅在 Visualforce 页面标记中?

You can have only one expression.你只能有一个表达式。 If you read documentation like https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.reference_directives如果您阅读https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.reference_directives 之类的文档

you'll see你会看到的

The expression can be a JavaScript identifier (for example, person) or dot notation that accesses a property from an object (person.firstName).表达式可以是 JavaScript 标识符(例如,person)或从对象 (person.firstName) 访问属性的点表示法。 The engine doesn't allow computed expressions (person 2 .name['John']).引擎不允许计算表达式(person 2 .name['John'])。 To compute the value of expression, use a getter in the JavaScript class.要计算表达式的值,请在 JavaScript 类中使用getter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM