我用laravel new test
创建了一个新的 Laravel 项目。然后我运行了 npm install
和 npm run dev
。我将 welcome.blade.php
文件更改为
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" href="{{ mix('js/app.js') }}"></script>
</head>
<body>
<div id="app">
<example-component></example-component>
</div>
</body>
</html>
这是我所做的唯一改变。但是该页面是空白的,并且 Chrome 上的 Vue devtools 扩展显示“未检测到 Vue.js”。
由于 Laravel 为 Vue 提供了几乎开箱即用的支持,我看不出哪里出了问题。相关文件如下(Laravel 的安装未受影响)。
/resources/js/app.js
(使用 npm run dev
成功编译为 /public/js/app.js
)
/**
* First we will load all of this project's JavaScript dependencies which
* includes Vue and other libraries. It is a great starting point when
* building robust, powerful web applications using Vue and Laravel.
*/
require('./bootstrap');
window.Vue = require('vue');
/**
* The following block of code may be used to automatically register your
* Vue components. It will recursively scan this directory for the Vue
* components and automatically register them with their "basename".
*
* Eg. ./components/ExampleComponent.vue -> <example-component></example-component>
*/
// const files = require.context('./', true, /\.vue$/i);
// files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default));
Vue.component('example-component', require('./components/ExampleComponent.vue'));
/**
* Next, we will create a fresh Vue application instance and attach it to
* the page. Then, you may begin adding components to this application
* or customize the JavaScript scaffolding to fit your unique needs.
*/
const app = new Vue({
el: '#app',
});
/resources/js/components/ExampleComponent.vue
:
<template>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Example Component</div>
<div class="card-body">
I'm an example component.
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
mounted() {
console.log('Component mounted.')
}
}
</script>
webpack.mix.js
:
const mix = require('laravel-mix');
/*
|--------------------------------------------------------------------------
| Mix Asset Management
|--------------------------------------------------------------------------
|
| Mix provides a clean, fluent API for defining some Webpack build steps
| for your Laravel application. By default, we are compiling the Sass
| file for the application as well as bundling up all the JS files.
|
*/
mix.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css');
package.json
:
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "npm run development -- --watch",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"axios": "^0.19",
"bootstrap": "^4.1.0",
"cross-env": "^5.1",
"jquery": "^3.2",
"laravel-mix": "^4.0.7",
"lodash": "^4.17.5",
"popper.js": "^1.12",
"resolve-url-loader": "^2.3.1",
"sass": "^1.15.2",
"sass-loader": "^7.1.0",
"vue": "^2.5.17",
"vue-template-compiler": "^2.6.10"
}
}
同样,这些都是在安装时就完成的。
有什么想法吗?空白页面成功加载编译后的 app.js
文件,看起来应该是这样(例如,提到了 Vue 和 example-component
)。
最佳答案
您需要 defer
你的脚本
<script src="{{ asset('js/app.js') }}" defer></script>
这样做的原因是:
const app = new Vue({
el: '#app',
});
Javascript 将尽快加载并且元素 #app
可能还没有加载。通过延迟,JS 将在文档加载后执行。
或者,您可以使用 jQuery 等待文档加载 $(document).ready
(但如果你在其他地方不需要它,请不要使用 jQuery)
没有 jQuery:https://stackoverflow.com/a/800010/8068675
编辑:正如@swonder 提醒我的那样,您还可以将脚本移动到正文底部。在这种情况下,它将在 DOM 完全加载后执行。
关于javascript - Vue.js 不适用于新的 Laravel 5.8 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56628788/