从Vue.js开始,想尝试一下laravel附带的例子 .
没有组件显示在我得到的控制台中
[Vue warn]: Failed to mount component: template or render function not defined.
found in
---> <Example>
<Root>
不是全新安装,从5.2-> 5.3-> 5.4升级
资源/资产/ 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');
/**
* 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.
*/
Vue.component('example', require('./components/Example.vue'));
const app = new Vue({
el: '#app'
});
资源/资产/ JS /组件/ Example.vue
<template>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Example Component</div>
<div class="panel-body">
I'm an example component!
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
mounted() {
console.log('Component mounted.')
}
}
</script>
这是我拥有js的刀片
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Testing</title>
<link rel="stylesheet" href="css/app.css">
<meta name="csrf-token" content="{{ csrf_token() }}">
</head>
<body>
<div id="app">
<example></example>
</div>
<script src="js/app.js" charset="utf-8"></script>
</body>
</html>
webpack.mix.js
let mix = require('laravel-mix');
mix.js('resources/assets/js/app.js', 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css');
5 回答
它与你安装Vue的方式有关 . 有一次它需要编译器而另一次不需要https://vuejs.org/v2/guide/installation.html#Runtime-Compiler-vs-Runtime-only .
您可以尝试我的方式(这是纯vue项目,由vue webpack模板制作):
这将在全局对象中安装组件 .
它应该是
require('./components/Example.vue').default
. 从v13开始,vue-loader
将组件导出为默认键,在使用import
时仍然可以正常工作,但在使用require
时需要使用上述键 .截至目前,我的石膏是为所有Vue对象的组件添加'default()' .
运行此包:
试试这个
我已经通过在 require().default 末尾使用 .default 方法轻松解决了这个问题 .