最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing $var inside @{{ }} in Vue.js + Laravel not working - Stack Overflow

programmeradmin1浏览0评论

I'm implementing Algolia Vue-InstantSearch in a Laravel Project. Since we’re editing a blade file, the {{ }} sequence will be interpreted by the php template engine. We can tell Blade to ignore those, to make sure they are only interpreted by Vue, in JavaScript, by prepending an @.

Taking this into consideration, I would like to pass a variable with a text inside @{{}}, but I cannot find the way. As an example I tried:

$a = "price ";

$var = $a . "result";

but when I print @{{$var}} , no value is displayed

This is my original code:

<ais-results>
    <template scope="{ result }">
            <!-- BEGIN item -->
            <div class="item">
                <div class="item-info">
                    <div class="item-price">@{{result.price}}</div>
                </div>
            </div>
            <!-- END item -->
        <!-- END col-2 -->
    </template>
</ais-results>

Any Help Appreciated.

I'm implementing Algolia Vue-InstantSearch in a Laravel Project. Since we’re editing a blade file, the {{ }} sequence will be interpreted by the php template engine. We can tell Blade to ignore those, to make sure they are only interpreted by Vue, in JavaScript, by prepending an @.

Taking this into consideration, I would like to pass a variable with a text inside @{{}}, but I cannot find the way. As an example I tried:

$a = "price ";

$var = $a . "result";

but when I print @{{$var}} , no value is displayed

This is my original code:

<ais-results>
    <template scope="{ result }">
            <!-- BEGIN item -->
            <div class="item">
                <div class="item-info">
                    <div class="item-price">@{{result.price}}</div>
                </div>
            </div>
            <!-- END item -->
        <!-- END col-2 -->
    </template>
</ais-results>

Any Help Appreciated.

Share Improve this question asked Oct 26, 2017 at 2:01 s_hs_h 1,4962 gold badges27 silver badges63 bronze badges 1
  • Could you update your example with what you actually tried? I don't see an issue with the current code you are showing. – rayrutjes Commented Oct 26, 2017 at 6:55
Add a ment  | 

2 Answers 2

Reset to default 6

It's a way to write Vue expression in Blade

As both Vue and Blade has their in-markup expression marked with {{}}, we need to differentiate the two.

Blade processes first and ignores the @{{}} expressions and removes the @ so that Vue can process it.

When laravel sees @{{}} it sees it as something it shouldn't process and removes the @, so ultimately you will be left with {{$var}} which will be processed by Vue, however, for Vue to process it, it must be part of a Vue instance, so in your example you would need something like:

new Vue({
  el: '#app', // or wherever you are mounting your instance
  data: {
    var: 'foo'
  }
});

In your case I would expect to be getting an:

Property or method "var" is not defined on the instance but referenced during render

message in your developers console, because you have placed your javascript variables outside of the vue instance itself.

发布评论

评论列表(0)

  1. 暂无评论