简体   繁体   English

如何在 vue 模板中按行拆分内联 JS 表达式?

[英]How to split inline JS expressions by lines in vue templates?

I clashed with the problem when had written this code:我在编写这段代码时遇到了这个问题:

<span
  class="deal-card__cost"
>{{ deal.total_cost | currency(
                        '₽',
                        0,
                        {
                          thousandsSeparator: ' ',
                          symbolOnLeft: false,
                          spaceBetweenAmountAndSymbol: true
                        }
                      ) | placeholder('- - -') }}</span>

And it can't be parsed.而且无法解析。 Is it possible in Vue templates to split inine JS expressions? Vue模板中是否可以拆分内联JS表达式? If yes - how?如果是 - 如何?

From the guide :从指南

In-template expressions are very convenient, but they are meant for simple operations.模板内表达式非常方便,但它们适用于简单的操作。 Putting too much logic in your templates can make them bloated and hard to maintain.在模板中放置过多的逻辑会使它们变得臃肿且难以维护。

For example:例如:

 <div id="example"> {{ message.split('').reverse().join('') }} </div>

At this point, the template is no longer simple and declarative.至此,模板不再是简单和声明性的了。 You have to look at it for a second before realizing that it displays message in reverse.在意识到它反向显示消息之前,您必须查看它一秒钟。 The problem is made worse when you want to include the reversed message in your template more than once.当您想在模板中多次包含反向消息时,问题会变得更糟。

That's why for any complex logic, you should use a computed property .这就是为什么对于任何复杂的逻辑,您都应该使用计算属性

I suggest you to use a computed property Computed我建议您使用计算属性Computed

 var vm = new Vue({ el: '#example', data: { message: 'Hello' }, computed: { // a computed getter reversedMessage: function () { // `this` points to the vm instance return this.message.split('').reverse().join('') } } })
 <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script> <div id="example"> <p>Original message: "{{ message }}"</p> <p>Computed reversed message: "{{ reversedMessage }}"</p> </div>

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

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