[英]Laravel blade.php use JavaScript compiled by mix to app.js
I'd like to use a JavaScript function in my blade.php file.我想在我的 Blade.php 文件中使用 JavaScript 函数。 I created the xyz.js file with a very simple function
我用一个非常简单的函数创建了 xyz.js 文件
function picture(soemthing){
document.getElementById('idXYZ').src = "example.com";
}
Then I added this file to the webpack.mix.js config:然后我将此文件添加到 webpack.mix.js 配置中:
mix.js(['resources/assets/js/app.js', 'resources/assets/js/xyz.js'], 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css');
and run: npm run dev.并运行:npm run dev。 So far so good, npm compiled my script and the picture function was included in the app.js file.
到目前为止一切顺利,npm 编译了我的脚本,并将图片功能包含在 app.js 文件中。 Now I'd like to add a form in blade.php and call the picture function on click.
现在我想在blade.php 中添加一个表单并在点击时调用图片函数。 I created the following blade.php file:
我创建了以下 Blade.php 文件:
@extends('layouts.app')
@section('content')
<div class="container">
<form>
Enter url: <input type="text" id="someID">
<input type="button" onclick="picture($('#someID').val())" value="Click"/>
</form>
</div>
<script src="{{ mix('js/app.js') }}"></script>
@endsection
And here I got really lost (btw: I was always the backend guy).在这里我真的迷路了(顺便说一句:我一直是后端的人)。 On page reload browser displays
在页面重新加载浏览器显示
[Vue warn]: Error compiling template: Templates should only be responsible for mapping the state to the UI. [Vue 警告]:编译模板时出错:模板应该只负责将状态映射到 UI。 Avoid placing tags with side-effects in your templates, such as , as they will not be parsed.
避免在模板中放置带有副作用的标签,例如 ,因为它们不会被解析。
And when I click the button I have当我单击按钮时
Error new:76 Uncaught ReferenceError: xyz is not defined新错误:76 未捕获的引用错误:未定义 xyz
I understand that the first errors says that the app.js file was not loaded and that's the reason why picture is not found.我知道第一个错误表示未加载 app.js 文件,这就是找不到图片的原因。 How should I include app.js or any other compiled by mix JavaScript file?
我应该如何包含 app.js 或任何其他由混合 JavaScript 文件编译的文件? Is it really the tag causing this problem?
真的是导致这个问题的标签吗?
I think you're getting the error because you're placing a new script tag inside of the vue.js application div.我认为您收到错误是因为您在 vue.js 应用程序 div 中放置了一个新的脚本标签。
If you're opening your layout file app.blade.php
there is a div with id="app"
and inside it you're having your content section yield.如果你打开你的布局文件
app.blade.php
有一个id="app"
的 div 并且在里面你有你的内容部分产量。 Side-effect means that you shouldn't load a new script inside a Vue.js template.副作用意味着您不应该在 Vue.js 模板中加载新脚本。
The script should be in your layout at the end of the body tag.该脚本应该在您的布局中 body 标记的末尾。 Also the script tag should look like this:
<script src="{{ asset('js/app.js') }}"></script>
脚本标签也应该是这样的:
<script src="{{ asset('js/app.js') }}"></script>
Then your script should be available.然后你的脚本应该可用。 But I would have a look at Vue.js and how you're handling click events there.
但我会看看 Vue.js 以及你如何处理那里的点击事件。 click handler docs
单击处理程序文档
Try this I solved me试试这个我解决了我
<script src="{{ asset('js/app.js') }}?time={{ time() }}"></script>
" I think not all browsers support this "
“我认为并非所有浏览器都支持这个”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.