简体   繁体   English

无法在php文件中调用javascript函数(Laravel)

[英]Cant call javascript function in php file (Laravel)

Im curently learning laravel with QR code. 我目前正在使用QR代码学习laravel。 I use the code that I got from github . 我使用从github获得的代码。 Its working fine when I run the html file. 当我运行html文件时,它的工作正常。 But when I add to my code in php file (Laravel) I get some error. 但是,当我在php文件(Laravel)中添加代码时,出现了一些错误。

The error: 错误:

ErrorException (E_ERROR) Call to undefined function formatName() (View: C:\\xampp\\htdocs\\museumadityawarman\\resources\\views\\topups\\scan_qrcode.blade.php) ErrorException(E_ERROR)调用未定义的函数formatName()(视图:C:\\ xampp \\ htdocs \\ museumadityawarman \\ resources \\ views \\ topups \\ scan_qrcode.blade.php)

The error points out to this part of my view: 错误指出了我的这一部分观点:

<span v-if="camera.id == activeCameraId" :title="formatName(camera.name)" class="active">{{ formatName(camera.name) }}</span>

<li v-for="scan in scans" :key="scan.date" :title="scan.content"><script type="text/javascript">{{scan.content}}</li>

I've tried to change from 我试图从

{{ formatName(camera.name) }}

To

<script>formatName(camera.name);</script>

This does fix the error message but I still can't get the value. 这确实可以修复错误消息,但是我仍然无法获取该值。

This is some of Javascript code: 这是一些Javascript代码:

 methods: {
    formatName: function (name) {
      return name || '(unknown)';
    },
    selectCamera: function (camera) {
      this.activeCameraId = camera.id;
      this.scanner.start(camera);
    }   }

Thanks, sorry for my bad english. 谢谢,抱歉我的英语不好。

You have to put an @ before the {{ formatName(camera.name) }} 您必须在{{formatName(camera.name)}}之前加上@

@{{ formatName(camera.name) }}

Look at the Laravel doc https://laravel.com/docs/5.6/blade#blade-and-javascript-frameworks : 查看Laravel文档https://laravel.com/docs/5.6/blade#blade-and-javascript-frameworks

Since many JavaScript frameworks also use "curly" braces to indicate a given expression should be displayed in the browser, you may use the @ symbol to inform the Blade rendering engine an expression should remain untouched 由于许多JavaScript框架还使用“大括号”来指示应在浏览器中显示给定的表达式,因此您可以使用@符号来通知Blade渲染引擎表达式应保持不变

EDIT: 编辑:

Since you look like you are using vue.js, you could also use the directive v-text like so : 由于您看起来正在使用vue.js,因此还可以像这样使用指令v-text:

<span v-if="camera.id == activeCameraId" :title="formatName(camera.name)" class="active" v-text="formatName(camera.name)"></span>

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

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