Laravel 5.4 Vue.JS Не удалось установить компонент: шаблон или функция рендеринга не определены
Начиная с 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 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');
Ответы
Ответ 1
Это связано с тем, как вы монтируете Vue. Один раз нужен компилятор, а другой нет https://vuejs.org/v2/guide/installation.html#Runtime-Compiler-vs-Runtime-only.
Вы можете попробовать мой способ (это чистый vue проект, созданный из шаблона vue webpack):
import Vue from 'vue'
import App from './App'
import Example from './components/Example'
import router from './router' //this will import router/index.js
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
template: '<App/>',
components: { App, Example }
})
Это будет монтировать компонент в глобальном объекте.
Ответ 2
Он должен require('./components/Example.vue').default
. Поскольку v13, vue-loader
экспортирует компонент в качестве ключа по умолчанию, который по-прежнему работает одинаково при использовании import
, но требует использования вышеуказанного при использовании require
.
Ответ 3
На данный момент, у меня есть штукатурка - добавить default() ко всем компонентам объекта Vue.
Vue.component('form-component', require('./components/FormComponent').default);
Vue.component('gratitude-pop', require('./components/GratitudePop').default);//..etc..etc
Запуск этого пакета:
"devDependencies": {
"axios": "^0.18",
"bootstrap": "^4.0.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"
},
"dependencies": {
"vuex": "^3.0.1"
}
Добавьте палец вверх для использования VUEX; P
Ответ 4
Попробуй это
Vue.component('example-component', require('./components/ExampleComponent.vue').default);
Ответ 5
Я решил эту проблему легко, используя
.default метод в конце
require().default
в следующей строке.
Vue.component('example-component', require('./components/ExampleComponent.vue').default);
Vue.component('articles', require('./components/ArticleComponent.vue').default);
app.js
/**
* First we will load all of this project 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-component', require('./components/ExampleComponent.vue'));
Vue.component('articles', require('./components/ArticleComponent.vue').default);
const app = new Vue({
el: '#app',
});
Ответ 6
Ответ 7
Вместо
Vue.component ('example', require ('./components / Example.vue'));
Используйте это
Vue.component ('example', require ('./components / Example.vue'). Default);