Vue component doesn't load - laravel

I want to load a vue component after I click on the "next" button. The project is laravel + vue.
The components load if I put them direct into laravel blade(you can see them below) but if I try to open component from the component it does'nt work. I don't get eny error it just stays the same component. The route in the address bar changes to the route that it should change... I mean it's really starnge.
app.js:
require('./bootstrap');
window.Vue = require('vue').default;
import Vue from 'vue';
import VueRouter from 'vue-router';
import { routes } from './routes';
Vue.use(VueRouter);
Vue.component('productinfo-index', require('./components/productInfo/index.vue').default);
Vue.component('audit-index', require('./components/audit/index.vue').default);
Vue.component('audit-info', require('./components/audit/auditInfo.vue').default);
const router = new VueRouter({
mode: 'history',
routes: routes
});
const app = new Vue({
el: '#app',
router: router
});
Routes.js:
import ProductInfoIndex from './components/productInfo/index';
import Audit from './components/audit/index';
import AuditInfo from './components/audit/auditInfo';
export const routes = [
{
path: '/productInfo',
name: 'ProductInfoIndex',
component: ProductInfoIndex
},
{
path: '/audit',
name: 'Audit',
component: Audit
},
{
path: '/audit/info',
name: 'AuditInfo',
component: AuditInfo
}
];
Index.vue:
<template>
<div>
<!-- Page Heading -->
<div class="d-sm-flex align-items-center justify-content-between mb-4">
<h1 class="h3 mb-0 text-gray-800">Inventorizacija</h1>
</div>
<div class="row">
<div class="card mx-auto">
<div class="card-header">
<router-link
:to="{name: 'AuditInfo'}"
class="btn btn-primary mb-2"
>Next</router-link>
</div>
<!-- <div v-if="showMessage">
<div class="alert alert-success">{{ message }}</div>
</div> -->
<div class="card-body">
<table class="table">
<thead>
<tr>
<th scope="col">Pavadinimas</th>
<th scope="col">Pradėta</th>
</tr>
</thead>
</div>
</div>
</div>
</div>
</template>
AuditInfo.vue(the component that should be loaded):
<template>
<div class="row">
<div class="card mx-auto">
<div class="card-header">
<router-link
:to="{name: 'Audit'}"
class="btn btn-primary mb-2"
>Back</router-link>
</div>
<div class="card-body">
<table class="table">
<thead>
<tr>
<th scope="col">Pavadinimas</th>
<th scope="col">Pradėta</th>
</tr>
</thead>
<tbody>
<tr>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</template>

You should put <router-view /> somewhere in the index.vue for rendering router component.

Related

How to integrate inline-template with VueJS?

I have little problem with display info with VueJS in inline template.
I have 1 component only with scripts
Answer.vue
<script>
export default {
props: ['answer'],
data(){
return{
editing: false
}
}
}
</script>
I register in app.js
import { createApp } from 'vue';
require('./bootstrap');
require('./fontawesome');
let app=createApp({})
app.component('user-info', require('./components/UserInfo.vue').default);
app.component('answer', require('./components/Answer.vue'));
app.mount("#app")
and i want to implement in answer.blade.php
<answer :answer="{{$answer}}">
<div class="media post">
#include ('shared._vote', [
'model' => $answer
])
<div class="media-body">
{!! $answer->body_html !!}
<div class="row">
<div class="col-4">
<div class="ml-auto">
#can('update',$answer)
Edit
#endcan
#can('delete', $answer)
<form method="POST" class="form-delete" action="{{route("questions.answers.destroy", [$question->id,$answer->id])}}">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure?')">Remove</button>
</form>
#endcan
</div>
</div>
<div class="col-4">
</div>
<div class="col-4">
<user-info :model="{{$answer}}" label="Answered"></user-info>
</div>
</div>
</div>
</div>
<hr>
</answer>
and i get this in console and this is answers which i cannot see:
https://prnt.sc/wof0gh
Thanks for help :)

Laravel vuejs 'Cannot read property of data' error

Been trying to figure this out for half an hour now and I cant seem to fix the error, here is my vue component:
<template>
<div class="container">
<div class="row mt-5">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h3 class="card-title">Country Lists</h3>
</div>
<!-- /.card-header -->
<div class="card-body table-responsive p-0">
<table class="table table-hover">
<tr>
<th>Year</th>
</tr>
<tr v-for="country in countries.data" :key="country.id">
<td>{{country.name}}</td>
</tr>
</table>
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
</div>
</div>
</div>
</template>
<script type="text/javascript" src="node_modules/vuejs/dist/vue.min.js"></script>
<script type="text/javascript" src="node_modules/vue-simple-search-dropdown/dist/vue-simple-search-dropdown.min.js"></script>
<script>
export default {
data(){
return{
country : {},
form: new Form({
id : '',
})
}
},
methods: {
loadCountry(){
axios.get('api/country').then(({data}) => (this.countries = data));
}
},
mounted() {
this.loadCountry();
Fire.$on('reloadAfter',() => {
this.loadCountry();
});
}
}
</script>
When I load the page the table wont display, but when I check the network from the developer tab in chrome, it can and loads the data, however it displays this error in the console 'Cannot read property 'data' of undefined'.
Thanks in advance.
The problem is here
<tr v-for="country in countries.data" :key="country.id">
<td>{{country.name}}</td>
</tr>
countries is not defined yet.
You can, for example, add it to data()
data(){
return{
countries : {},
},
}
And test the presence of data
<table v-if="countries.data" class="table table-hover">

i try to mount a component in a component vue

i i try to mount a component in a component, this component its a partial, in especifict its a paginator, which i need to integrate, i use props in the paginate component.
but i have a problem, in the console appears the next messagge "Failed to mount component: template or render function not defined." i am new in vue js, i using router-view i don´t know if this
it is affecting en the problem the code its the next :
Pedido.vue
<template>
<div id="pedido" style="margin-top:50px">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h4 class="card-title">Pedidos</h4>
<div class="card-tools" style="position: absolute;right: 1rem;top: .5rem;">
<button type="button" class="btn btn-info" >
Nuevo
<i class="fas fa-plus"></i>
</button>
<button type="button" class="btn btn-primary" >
Recargar
<i class="fas fa-sync"></i>
</button>
</div>
</div>
<div class="card-body">
<div class="mb-3">
<div class="row">
<div class="col-md-2">
<strong>Buscar por :</strong>
</div>
<div class="col-md-3">
<select class="form-control" id="fileds">
<option value="total">Codigo</option>
<option value="name">Nombre</option>
<option value="email">Apellido</option>
<option value="phone">Telefono</option>
<option value="address">Direccion</option>
</select>
</div>
<div class="col-md-7">
<input type="text" class="form-control" placeholder="Buscar">
</div>
</div>
</div>
<div class="table-responsive">
<table class="table table-hover table-bordered table-striped">
<thead>
<tr>
<th scope="col">Codigo</th>
<th scope="col">Nombre</th>
<th scope="col">Apellido</th>
<th scope="col">Telefono</th>
<th scope="col">Rut</th>
<th scope="col" class="text-center">Action</th>
</tr>
</thead>
<tbody>
<tr v-for="(pedido, index) in pedidos" :key="pedido.codigo">
<th scope="row">{{ index + 1}}</th>
<td>{{ pedido.nombre_cliente}}</td>
<td>{{ pedido.apellido_cliente }}</td>
<td>{{ pedido.telefono_cliente}}</td>
<td>{{ pedido.rut_cliente }}</td>
<td class="text-center">
<button type="button" class="btn btn-info btn-sm">
<i class="fas fa-eye"></i>
</button>
<button type="button" class="btn btn-primary btn-sm">
<i class="fas fa-edit"></i>
</button>
<button
type="button"
class="btn btn-danger btn-sm"
>
<i class="fas fa-trash-alt"></i>
</button>
</td>
</tr>
<tr >
<td colspan="6">
<div class="alert alert-danger" role="alert">No se ah encontrado resultados :(</div>
</td>
</tr>
</tbody>
</table>
<div class="card-footer">
<pagination
v-if="pagination.last_page > 1"
:pagination="pagination"
:offset="5"
#paginate="getData()"
></pagination>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
data(){
return{
pedidos:[],
pagination: {
current_page: 1,
},
}
},
mounted() {
console.log('Component mounted.')
this.getData();
},
methods:{
getData(){
this.$Progress.start();
axios.get("api/pedidos?page=" + this.pagination.current_page)
.then(response =>{
this.pedidos = response.data.data;
this.pagination = response.data.meta;
this.$Progress.finish();
})
.catch(e =>{
console.log(e)
this.$Progress.fail();
})
//.then(({ data }) => (this.pedidos = data));
}
},
}
</script>
this its PaginationComponent.vue:
<template>
<nav aria-label="...">
<ul class="pagination justify-content-center">
<li class="page-item" :class="{ disabled: pagination.current_page <= 1 }">
<a class="page-link" #click.prevent="changePage(1)" >First page</a>
</li>
<li class="page-item" :class="{ disabled: pagination.current_page <= 1 }">
<a class="page-link" #click.prevent="changePage(pagination.current_page - 1)">Previous</a>
</li>
<li class="page-item" v-for="page in pages" :key="page" :class="isCurrentPage(page) ? 'active' : ''">
<a class="page-link" #click.prevent="changePage(page)">{{ page }}
<span v-if="isCurrentPage(page)" class="sr-only">(current)</span>
</a>
</li>
<li class="page-item" :class="{ disabled: pagination.current_page >= pagination.last_page }">
<a class="page-link" #click.prevent="changePage(pagination.current_page + 1)">Next</a>
</li>
<li class="page-item" :class="{ disabled: pagination.current_page >= pagination.last_page }">
<a class="page-link" #click.prevent="changePage(pagination.last_page)">Last page</a>
</li>
</ul>
</nav>
</template>
<script>
export default {
props:['pagination', 'offset'],
methods: {
isCurrentPage(page){
return this.pagination.current_page === page
},
changePage(page) {
if (page > this.pagination.last_page) {
page = this.pagination.last_page;
}
this.pagination.current_page = page;
this.$emit('paginate');
}
},
computed: {
pages() {
let pages = []
let from = this.pagination.current_page - Math.floor(this.offset / 2)
if (from < 1) {
from = 1
}
let to = from + this.offset -1
if (to > this.pagination.last_page) {
to = this.pagination.last_page
}
while (from <= to) {
pages.push(from)
from++
}
return pages
}
}
}
</script>
app.js
Vue.component('pagination', require('./components/partial/PaginationComponent.vue'));
const app = new Vue({
el: '#app',
router
});
this its the error
but in the extension of vue in the console i see the
properties of the object, this is fine,
but I do not know what I'm doing wrong, like I said I'm new to this.
extension of vue
I hope they understand me,
I would greatly appreciate your help.
Pedido.vue
export default {
components: { pagination },
data(){ ....
maybe is this problem
On the file app.js you apart from especify where you are mounting your component (in this case on the div with id "app"), you also need to tell Vue what to render.
Your component on the app.js file should include a template or a render function, that's why you are getting the error "Failed to mount component: template or render function not defined.
You can add it like this:
const app = new Vue({
el: '#app',
router,
template: '<pagination/>'
});
Or using the render function like this:
import Pagination from './components/partial/PaginationComponent.vue'
const app = new Vue({
el: '#app',
router,
render: (h) => h(Pagination)
});
well, i tried with their answer, but dont worked ,
I managed to solve it by adding:
Vue.component('pagination', require('./components/partial/PaginationComponent.vue').default);

Info not displaying in modal, however no errors and Vue component works in console

Looking to display data in a modal. My set up is as follows:
app.js
Vue.component('modal', require('./components/Modal.vue'));
const app = new Vue({
el: '#vue',
data() {
return {
id: '',
booking_start: '',
booking_end: '',
car: [],
user: []
};
},
});
Modal.vue component:
<template>
<div id="modal" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<slot name="header"></slot>
</div>
<div class="modal-body">
<slot></slot>
</div>
<div class="modal-footer">
<slot name="footer"></slot>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</template>
<script>
export default {
name: 'modal',
mounted() {
console.log('Modal mounted.')
},
data() {
return {}
},
props: ['id', 'booking_start', 'car', 'user'],
mounted() {
}
}
</script>
Laravel blade:
<div id="vue">
<modal v-bind="{{json_encode($reservation)}}">
<template slot="header">
<strong>Vehicle Checkout</strong>
</template>
<p>Ready to check out this vehicle?</p>
<table class="table table-sm">
<tr>
<th>Vehicle Name</th>
<td><span id="reservation-car-name">#{{ car.name }}</span></td>
</tr>
<tr>
<th>Vehicle Make / Model</th>
<td><span id="reservation-car-make-model"></span></td>
</tr>
<tr>
<th>Vehicle Registration</th>
<td><span id="reservation-car-registration"></span></td>
</tr>
<tr>
<th>Odometer Start</th>
<td><span id="reservation-car-odometer"></span></td>
</tr>
</table>
<template slot="footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Checkout</button>
</template>
</modal>
</div>
At this point, I am just attempting to get the data to show in the modal.
Looking at the Vue Dev tools:
There are no errors in the console, and I can output the data I am after in the console.
I'm probably missing something very basic as I am new to Vue, but I can't for the life of me work out what it is.
Component tag replaced by template content, so put all content into modal component from component tag <modal>.your content.</modal>
Vue.component('modal',{
props:['car'],
template: `<div><template slot="header">
<strong>Vehicle Checkout</strong>
</template>
<p>Ready to check out this vehicle?</p>
<table class="table table-sm">
<tr>
<th>Vehicle Name</th>
<td><span id="reservation-car-name">{{ car.name }}</span></td>
</tr>
<tr>
<th>Vehicle Make / Model</th>
<td><span id="reservation-car-make-model">{{ car.model }}</span></td>
</tr>
<tr>
<th>Vehicle Registration</th>
<td><span id="reservation-car-registration">{{ car.reg }}</span></td>
</tr>
<tr>
<th>Odometer Start</th>
<td><span id="reservation-car-odometer">{{ car.odo }}</span></td>
</tr>
</table>
<template slot="footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Checkout</button>
</template>
</div>`
})
const app = new Vue({
el: '#vue',
data() {
return {
id: '',
booking_start: '',
booking_end: '',
car: {name:'test1',model:'gh201',reg:'201821542',odo:'2018-01-01'},
user: []
};
},
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.0/js/bootstrap.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.0/css/bootstrap.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<div id="vue">
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<modal :car="car"></modal>
</div>
</div>
</div>
</div>
My other answers related this issue : router view and transition-group

Pagination problems in Vue and Laravel

Here is a component I've made for my Vue app within a laravel project.
<template>
<div>
<h2 class="text-center mt-3 mb-5">Categories</h2>
<form v-on:submit.prevent="addCategory">
<div class="row">
<div class="col-2"></div>
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control" v-model="name.name">
</div>
</div>
<div col-2>
<button class="ml-5 btn btn-primary" v-on:submit.prevent="addCategory">Add New Tag</button>
</div>
<div class="col-2"></div>
</div>
</form>
<div class="row">
<div class="col-2"></div>
<div class="col-md-8">
<br/>
<table class="table table-hover">
<thead>
<tr class="alert-info">
<td>ID</td>
<td>Category Name</td>
<td style="width: 25%">Actions</td>
</tr>
</thead>
<tbody>
<tr v-for="category in categories.data">
<td>{{ category.id }}</td>
<td>{{ category.name }}</td>
<td>
<router-link :to="{name: 'EditCategory', params: { id: category.id }}"
class="btn btn-primary btn-sm ml-auto">Edit
</router-link>
<button class="btn btn-danger btn-sm" v-on:click="deleteCategory(category.id)">Delete
</button>
</td>
</tr>
</tbody>
</table>
<vue-pagination :pagination="categories" #paginate="fetchCategories()" :offset="4"></vue-pagination>
<hr class="custom-hr-divider"/>
</div>
<div class="col-2"></div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
categories:{
total: 0,
per_page: 2,
from: 1,
to: 0,
current_page: 1
},
name: {}
}
},
created: function () {
this.fetchCategories();
},
components:{
VuePagination: require('../components/Pagination.vue')
},
methods: {
fetchCategories() {
this.axios.get('/categories/index?page=' + this.current_page)
.then(function (response) {
this.users = response.data.data;
this.pagination = response.data;
})
.catch(() => {
console.log('handle server error from here');
});
},
deleteCategory(id) {
let uri = `http://localhost:8000/categories/delete/${id}`;
this.axios.delete(uri);
this.fetchCategories();
},
addCategory() {
let uri = 'http://localhost:8000/categories';
this.axios.post(uri, this.name).then((response) => {
this.fetchCategories();
this.name = {};
})
}
}
};
</script>
Here is the Pagination.vue file I found in an online tutorial.
My app works fine without pagination but introducing this code causes the following errors.
***[Vue warn]: Failed to mount component: template or render function not defined.
found in
---> <VuePagination>
<DisplayCategory> at resources\assets\js\components\DisplayCategory.vue
<Root>***
Where do I define define the component? Ive been trying to get this to work for hours.
app.js
import Vue from 'vue';
import VueRouter from 'vue-router';
Vue.use(VueRouter);
import VueAxios from 'vue-axios';
import axios from 'axios';
Vue.use(VueAxios, axios);
import App from './App.vue';
import DisplayTag from './components/DisplayTag.vue';
import EditTag from './components/EditTag.vue';
import DisplayCategory from './components/DisplayCategory.vue';
import EditCategory from './components/EditCategory.vue';
const routes = [
{
name: 'DisplayTag',
path: '/tags',
component: DisplayTag
},
{
name: 'EditTag',
path: '/tags/edit/:id',
component: EditTag
},
{
name: 'DisplayCategory',
path: '/categories',
component: DisplayCategory
},
{
name: 'EditCategory',
path: '/categories/edit/:id',
component: EditCategory
}
];
const router = new VueRouter({ mode: 'history', routes: routes});
new Vue(Vue.util.extend({ router }, App)).$mount('#app');
Anyone with any ideas what I'm doing wrong?
Thanks in advance.
Achieved pagination by using laravel-vue-pagination
Getting started is easy to follow and I only had to change 1 line as I am using axios.
fetchCategories(page) {
if (typeof page === 'undefined') {
page = 1;
}
// Using vue-resource as an example
let uri = `http://localhost:8000/categories/index?page=`;
this.axios.get(uri + page).then((response) =>
{this.categories = response.data;})

Resources