My app seems to be loading without errors except the router does not load the components:
// routes.js
import Login from './components/Login.vue'
const routes = [
{ path: '/', component: Login }
]
export default routes
// vue-config.js
import Vue from 'vue';
import VueRouter from 'vue-router'
import routes from './routes'
Vue.use(VueRouter)
const router = new VueRouter({
routes,
mode: 'history'
})
const app = new Vue({
router
}).$mount('#root')
// app.js
require('./vue-config');
I'm using laravels frontend JS setup
Looks like router is loading fine but nor recognizing components:
// Login.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">Login</div>
<div class="panel-body">
<form class="form-horizontal" #submit.prevent="login">
<div class="form-group" :class="{ 'has-error': error }">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input v-model="email" id="email" type="email" class="form-control" name="email" required autofocus>
</div>
</div>
<div class="form-group" :class="{ 'has-error': error }">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input v-model="password" id="password" type="password" class="form-control" name="password" required>
<span v-show="error" class="help-block">
<strong>{{ error }}</strong>
</span>
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Login
</button>
<a class="btn btn-link" href="#">
Forgot Your Password?
</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import * as api from '../api'
import swalUtils from '../swalUtils'
export default {
data() {
return {
email: '',
password: '',
error: '',
}
},
methods: {
login() {
utils.spinner();
api.login(this.email, this.password)
.then(resp => {
if(resp.data.access_token) {
localStorage.setItem('token', resp.data.access_token);
// redirect
}
})
.catch(err => { console.log(err); swalUtils.handleError(err); } ); }
},
}
</script>
The vue router mounts the components you define in your routes in the <router-view> tag/component. Most likely you want this in your App.vue or whatever you use as root component. You would end up with something like this:
// App.vue
<template>
<div id="app">
<router-view />
</div>
</template>
<script>
export default {
name: "App"
};
</script>
// main.js
import Vue from 'vue';
import VueRouter from 'vue-router'
import routes from './routes'
import App from './App'
Vue.use(VueRouter)
const router = new VueRouter({
routes,
mode: 'history'
})
const app = new Vue({
router,
components: { App },
template: '<App />'
}).$mount('#app')
Notice that two things have changed in this code. The first is that we are now defining a router-view in our main component, namely App. The second is that we use this component to mount it in main.js.
Related
I have an error on login form after upgrading version Vue JS, Axios, Laravel.
This project code working well with old version. Anyone can explain this serious errors.
Thanks you for your help!
And I stuck with it many days. I've search for similar post error, but only seen this(POST http://127.0.0.1:8000/ 422 (Unprocessable Entity)).
<template>
<div class="back-img" :style="`background-image: url('${appUrl}/images/background/default-background.jpg')`">
<div class="container-fluid">
<div class="row">
<div class="col-12 col-md-6 offset-md-6 col-lg-4 offset-lg-8 col-xl-4 offset-xl-8 pl-0">
<div class="sign-in-sign-up-content">
<form class="sign-in-sign-up-form">
<div class="text-center mb-4 application-logo">
<img :src="publicPath+'/uploads/logo/'+appLogo" alt="" class="img-fluid logo">
</div>
<div class="form-row">
<div class="form-group col-12">
<h3 class="text-center mb-0">
{{ trans('lang.hi_there') }}
</h3><br>
<label class="text-center d-block">
{{ trans('lang.sign_in_to_your_dashboard') }}
</label>
</div>
</div>
<div v-if="alertMessage.length>0" class="alertBranch">
<div class="alert alert-warning alertBranch" role="alert">
{{alertMessage}}
</div>
</div><br>
<div class="form-row">
<div class="form-group col-12">
<label for="username" style="font-size: 20px;"> {{ trans('lang.login_username') }}</label>
<input id="username"
v-validate="'required'"
v-model="username"
type="text"
name="username"
class="form-control"
:placeholder="trans('lang.enter_username')"
:class="{ 'is-invalid': submitted && errors.has('username') }">
<div class="heightError" v-if="submitted && errors.has('username')">
<small class="text-danger" v-show="errors.has('username')">
{{ errors.first('username') }}
</small>
</div>
</div>
</div>
<div class="form-row">
<div class="form-group col-12">
<label for="password" style="font-size: 20px !important;">{{ trans('lang.login_password') }}</label>
<input id="password"
v-validate="'required'"
ref="password"
v-model="password"
name="password"
type="password"
class="form-control"
:placeholder="trans('lang.enter_password')"
:class="{ 'is-invalid': submitted && errors.has('password') }">
<div class="heightError">
<small class="text-danger" v-show="errors.has('password')">
{{ errors.first('password') }}
</small>
</div>
</div>
</div>
<div class="form-row loginButton">
<div class="form-group col-12">
<common-submit-button class="btn-block text-center auth-button"
style="font-size: 20px !important;"
:buttonLoader="buttonLoader"
:isDisabled="isDisabled"
:isActiveText="isActiveText"
buttonText="login"
v-on:submit="loginPost"/>
</div>
</div>
<!-- <div class="form-row">
<div class="form-group col-12">
<a href="#"
#click="forgetPassword"
class="bluish-text">
<i class="fa fa-lock"/>
{{ trans('lang.forgot_password') }}
</a>
</div>
</div> -->
</form>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import axiosGetPost from '../../helper/axiosGetPostCommon';
export default {
extends: axiosGetPost,
props: ['checkusername', 'checkpass'],
data() {
return {
username: this.checkusername,
name: '',
password: this.checkpass,
remember: true,
buttonLoader: false,
isActiveText: false,
isDisabled: false,
preLoaderType: 'load',
hidePreLoader: false,
isActive: 'active',
alertMessage: '',
submitted: false,
}
},
methods: {
loginPost() {
this.submitted = true,
this.$validator.validateAll().then((result) => {
if (result) {
this.inputFields = {
username: this.username,
password: this.password,
};
this.buttonLoader = true;
this.isDisabled = true;
this.isActiveText = true;
this.loginPostMethod('/', {
username: this.username,
password: this.password,
}
);
}
});
},
forgetPassword() {
let instance = this;
instance.redirect('/password/reset');
},
loginPostSucces(response) {
let instance = this;
instance.redirect("/sales");
},
loginPostError(response) {
let instance = this;
instance.buttonLoader = false;
instance.isDisabled = false;
instance.isActiveText = false;
instance.alertMessage = response.data.errors.username[0];
},
}
}
</script>
File: resources\assets\js\js\bootstrap.js
window._ = require('lodash');
window.Popper = require('popper.js').default;
import axios from "axios";
try {
window.$ = window.jQuery = require('jquery');
require('bootstrap');
} catch (e) {}
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
if (document.querySelector('meta[name="csrf-token"]')) {
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
}
You have to pass the csrf token in Ajax request.
data: {
"_token": "{{ csrf_token() }}",
other form data
}
OR
You can create csrf token in header using meta element and access that value in while sending ajax request.
<meta name="csrf-token" content="{{ csrf_token() }}" />
// Access in ajax request like below
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
Laravel + Vue + Axios
Laravel has configurd the default token header in resources/js/bootstrap.js
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
So you have to just import axios like.
import axios from 'axios';
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 :)
I have login reactive form and added validations to form fields.
Once the user entered the value in form fields it should update the form value on the screen as I'm displaying form.value and also it should display the form status too. Both form value and status are not changing.
Nothing happened.
html
<div class="jumbotron">
<div class="container">
<div class="row">
<div class="col-md-6 offset-md-3">
<h2>Login</h2>
<form class='form' [formGroup]='loginForm' (ngSubmit)='onSubmit()' novalidate>
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" placeholder="Username"/>
<div *ngIf="f.username.touched && f.username.errors" class="invalid-feedback">
<div *ngIf="f.username.errors.required">Username is required</div>
</div>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" placeholder="Password"/>
<div *ngIf="f.password.touched && f.password.errors" class="invalid-feedback">
<div *ngIf="f.password.errors.required">Password is required</div>
</div>
</div>
<div class="form-group">
<label><input type="checkbox"> Remember me</label>
</div>
<div class="form-group">
<button [disabled]="loginForm.invalid" class="btn btn-primary">Login</button>
</div>
</form>
<p>Form value: {{loginForm.value | json}}</p>
<p>Form value: {{loginForm.status | json}}</p>
</div>
</div>
</div>
</div>
ts file
import { ObjectMethod } from '#babel/types/lib';
import { AuthService } from './../auth.service';
import { Component, OnInit } from '#angular/core';
import { FormBuilder, FormGroup, FormControl, Validators } from '#angular/forms';
#Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
loginForm: FormGroup;
returnUrl: string;
constructor(
private formBuilder: FormBuilder,
private authService: AuthService) { }
ngOnInit() {
this.loginForm = this.formBuilder.group({
username: ['', [ Validators.required]],
password: ['', [ Validators.required]]
});
}
//convenience getter for easy access to form fields
get f() {
return this.loginForm.controls;
}
onSubmit() {
console.log(this.loginForm.value);
/*Object.keys(this.loginForm.value).map(e => {
console.log(">>>>>>>",this.loginForm.value[e]);
});*/
this.authService.getUserDetails();
}
}
Because of form is invalid, login button is also not enabled.
Please help me in solving this.
You need to add formControlName to your inputs such as:
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" formControlName="username" placeholder="Username"/>
<div *ngIf="f.username.touched && f.username.errors" class="invalid-feedback">
<div *ngIf="f.username.errors.required">Username is required</div>
</div>
</div>
For validity check I would use loginform.valid, this should return false when the form is invalid
I'm trying to make an user authentication using laravel as back-end and vue.js as front-end, and it happens that after adding the cors middleware, laravel still doesn't get the post requests from vue.js (it works for get requests).
I'm using passport for the authentication and on the console I get
a 404 error...
I implemented vue.resource, cors, and passport (since I have the tables generated on the database by it and the new routes that passport implements) as it should be done, yet if there is some code that I did not upload in here that might help to get the solution to this problem, please feel free to ask.
Here is the code of the vue components...
Main.js:
import Vue from 'vue'
import App from './App.vue'
import Router from './routes.js'
import VueResource from 'vue-resource'
Vue.use(VueResource)
new Vue({
el: '#app',
render: h => h(App),
router: Router
})
App.vue:
<template>
<div class="container" id="app">
<bar></bar>
<router-view></router-view>
</div>
</template>
<script>
import bar from './components/bar.vue'
var App = {
csrfToken: "{{ csrf_token() }}"
}
export default {
components: {
'bar': bar,
}
}
</script>
<style>
...
</style>
login.vue (Here is the post method):
<template>
<div class="container">
<div class="row" id="pwd-container">
<div class="col-md-4"></div>
<div class="col-md-4">
<section class="login-form">
<form method="post" action=# role="login">
<input v-model="email" type="email" name="email" placeholder="Email" required class="form-control input-lg" value="joestudent#gmail.com" />
<input v-model="password" type="password" class="form-control input-lg" id="password" placeholder="Password" required="" />
<div class="pwstrength_viewport_progress"></div>
<!--type="submit"-->
<button #click="login" type="submit" name="go" class="btn btn-lg btn-primary btn-block">Sign in</button>
<div>
Create account or reset password
</div>
</form>
<div class="form-links">
www.website.com
</div>
</section>
</div>
<div class="col-md-4"></div>
</div>
</div>
</template>
<script>
export default {
data(){
return {
email: '',
password: ''
}
},
methods: {
login(){
var data = {
grant_type: 'password',
client_id: 2,
client_secret: '1NB8ybcjuc1nOp70uwhJ7KygaK0AdsUHzroQuH7M',
username: this.email,
password: this.password,
scope: ''
}
this.$http.post('http://localhost:8000/oauth/token', data)
.then(response =>{
console.log(response.data)
})
.catch(response =>{
// Error array
})
}
}
}
</script>
<style>
</style>
I've a password_reset.vue script as below:
<template>
<form>
<div class="alert alert-danger" v-if="form.errors.has('photo')">
#{{ form.errors.get('photo') }}
</div>
<div class="row">
<div class="col-md-3">
<div class="label">Current Password</div>
</div>
<div class="col-md-9">
<div class="field--wrapper">
<label for="current_password">Current Password</label>
<input class="nom-fields" type="password"
v-model="form.current_password"
id="current_password" name="current_password"
placeholder="Current Password" value="">
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="label">Repeat Password</div>
</div>
<div class="col-md-9">
<div class="field--wrapper">
<label for="password_repeat">Repeat Password</label>
<input class="nom-fields" type="password"
v-model="form.password_repeat"
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-9 text-right">
<button id="change_password"
class="blue-btn" v-on:click="update">
Change Password
</button>
</div>
</div>
</form>
</template>
<script>
export default{
data(){
return{
form: new MyForm({
current_password: '',
new_password: '',
password_repeat: '',
message: ''
})
};
},
methods: {
/**
* Update the user's password.
*/
update(e) {
e.preventDefault();
this.form.startProcessing();
this.$http.post('/account/change_password', this.form)
.then(function(response) {
this.form.finishProcessing();
})
.catch(function(response) {
this.form.setErrors(response.data);
});
},
}
}
</script>
and in my bootstrap.js file, I've:
import './interceptors.js';
import './forms/form.js';
import './forms/errors.js';
import PasswordReset from './settings/password_reset.vue'
new Vue({
el: 'body',
components: {
PasswordReset
},
ready() {
}
});
and in the app.js file I've:
import './bootstrap.js'
HTML:
<body>
<password-reset></password-reset>
</body>
But it seems the component is not getting displayed. I tried to alert a msg in the ready method and it's working fine. I'm loading Vue.js also.