vue js navigate to url with question mark - vue.js

my Vue js project login template click button it redirects to like this
http://localhost:8080/#/login to http://localhost:8080/?#/login
both Url show interface.
1st Url not set the local storage variable 'token'.
2nd Url is set the local storage variable 'token'.
how to solve it?
Login.vue
<template>
<div class="row col-sm-12">
<form>
<div class="form-group">
<label>Email address:</label>
<input type="email" class="form-control" v-model="email">
</div>
<div class="form-group">
<label>Password:</label>
<input type="password" class="form-control" v-model="password">
</div>
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div>
<button #click="login" type="submit" class="btn btn-success">Submit</button>
</form>
</div>
</template>
<script>
export default{
data(){
return{
email:'',
password:''
}
},
methods: {
login(){
var data = {
email:this.email,
password:this.password
}
this.$http.post("api/auth/login",data)
.then(response => {
this.$auth.setToken(response.body.token)
// console.log(response)
})
}
}
}
</script>

The form is getting submitted as the button you have provided in the form has type="submit" which is the default behaviour of a button present inside form even if you do not add the attribute type="button"
So replace the type submit to button o prevent form submission like this:
<button #click="login" type="button" class="btn btn-success">Submit</button>

I just faced the same issue, and the provided solution did not worked for me (Vue 2.5.17).
I had to add a modifier to the #click event to prevent the default behavior:
<button class="btn btn-primary" #click.prevent="login">Login</button>

Changing the button type to button, you disable default form behaviour, like pressing enter and release submit event.
Just add a modifier to the #submit or #reset event to prevent default behaviour:
<b-form #submit.prevent="onSubmit" #reset.prevent="onReset">

In order to keep the default behaviour of submitting the form using the "Enter" key on the keyboard as well, another solution is:
to keep the type = 'submit' on the button
handle the event parameter on the callback and process it with event.preventDefault(); like that:
login(event){
event.preventDefault()
var data = {
email:this.email,
password:this.password
}
...
}
Event.preventDefault() will prevent the default behaviour which is here to submit the form on the current url with the issue we are dealing with here.
See https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault

Related

how to call method on petite-vue/VueJS component

I have a very small vue-petite component, with following template.
When button is clicked outside, I want to call method clear() on component letting it clear itself.
<template id="input-template">
<input type="text" v-model="keyword">
</template>
<div v-scope>
<div v-scope="model"></div>
<button #click="model.clear()">
</div>
createApp({model:
{
$template: '#input-template',
keyword: '',
clear() {
this.keyword = '';
}
}}).mount();

Vue / Buefy Datetimpciker: How can I submit the results of an vue compontent to a form?

For my new website project I wanted to use a datetime picker in a html form. As i use Bulma as CSS framework, I want to use the buefy datetimepicker https://buefy.org/documentation/datetimepicker, which is using vue.
For now, the datetimepicker is displayed nicely on my website, however, I am not able to receiving results when submitting the html form by a POST request.
What do I need to change to make the selected datetime available as a variable in php $_POST variable?
Code on codepen
<form action="submit.php" method="POST">
<div id="app" class="container">
<b-field label="Select datetime">
<b-datetimepicker
rounded
placeholder="Click to select..."
icon="calendar-today"
:locale="locale"
:datepicker="{ showWeekNumber }"
:timepicker="{ enableSeconds, hourFormat }"
horizontal-time-picker>
</b-datetimepicker>
</b-field>
</section>
</div>
<div class="field">
<div class="control">
<button class="button is-primary">Submit</button>
</div>
</div>
</form>
<script>
const example = {
data() {
return {
showWeekNumber: true,
enableSeconds: false,
hourFormat: undefined, // Browser locale
locale: undefined // Browser locale
}
}
}
const app = new Vue(example)
app.$mount('#app')
</script>
You should bind datetimepicker to a variable normally with v-model, then assign that variable to a hidden input field.
In HTML file
<input type="hidden" name="mydate" value="{{ mydate }}" />
<b-datetimepicker
v-model="mydate"
...>
</b-datetimepicker>
In JS file
data() {
...,
mydate: null
}

VueJS - How to Enable/Disable a specific input in a v-for loop

I need to enable/disable specific inputs on button click.
My problem here is when I click my button, ALL the inputs enable/disable I am having a hard time targeting a single one.
I use props “articles” which returns the “id”, “title” and “body” of my articles.
<div v-for="(article, index) in articles" v-bind:key="article.id">
<div class="flex">
<div class="flex-1 px-2 py-2">
<input
v-model="article.title"
type="text"
:disabled="isDiabled"
class="w-full disabled:opacity-75 bg-gray-300 focus:bg-white"
/>
</div>
<div class="flex-1 px-2 py-2">
<input
v-model="article.body"
type="text"
:disabled="isDiabled"
class="w-full disabled:opacity-75 bg-gray-300 focus:bg-white"
/>
</div>
<div class="px-2 py-2">
<button
class="px-2 py-2 border-gray-400 bg-gray-800 rounded text-white hover:border-gray-300 items-end"
#click="enableEdit"
>
Edit
</button>
</div>
</div>
</div>
<script>
export default {
props: ["articles"],
data() {
return {
isDiabled: true,
};
},
methods: {
enableEdit() {
this.isDiabled = false;
},
},
};
</script>
Every article has his own button and I want only the two inputs of the button that is clicked to be enabled/disabled and not all of them.
As being pointed you need to add also that property to your articles... of course you don't need to do this in the database, still using it as a property you need also to emit this change of the flag to the parent.
You could also use a local version of the articles where you add also this isDisabled property, you fill this new variable at the creation of the component based of course on the property you received from the parent,
created(){
this.local_articles = this.articles.map(e => {
return {...e, isDisabled: true} }
)
},
this way you don't need to propagate nothing to the parent as you can handle all internally.
This fiddle gives a solution to your problem.
Make a boolean property for each article, then change the binding of disabled to that property.

Don't see div with v-if, regardless of the value in the data

I have problem with displaying component with v-if.
In one component i have <div v-if="seen">...</div>.
In another component I have <button v-on:click="seen = !seen">...</button>.
In "var vue = nev Vue({...})" file, in data: I have seen: true and this is not working.
I found "solution" which works: example
and I tried this "function version" of data in my code, but it doesn't work too :/
Here is my code:
Main File
var vue = new Vue({
el: '#app',
components: {
Navigation,
Home,
Footer,
Login
},
data: function () {
return {
seen: true
}
},
template: "<div><navigation></navigation><login></login><home></home><Footer></Footer></div>"
})
template that I can't see
<div v-if="seen" id="loginbox">
<form>
<input type="text" placeholder="login" class="input is-rounded"/>
<input type="password" placeholder="password" class="input is-rounded"/>
</form>
</div>
button template
<div class="navbar-menu">
<div class="navbar-start"></div>
<div class="navbar-end">
<p class="nav-item">
<a class="nav-link" v-on:click="seen = !seen">Login</a>
</p>
<p class="nav-item">
<a class="nav-link">Register</a>
</p>
</div>
</div>
I expect that when I click on button, "loginbox" template will be shown.
EDIT:
I did it in half way. I used props (used export default...) in in template that I cannot seen. It not work properly, becouse now I can change value of "seen" only with button which is in this template. I'd like change value of it by button which is in another template.
You should somehow share data between components.
You can do it many ways, but for this case i suggest to use event handling https://v2.vuejs.org/v2/guide/events.html#ad
Edited your sandbox with example - https://codesandbox.io/s/mzq0r2w88j

Select box on change event in Bootstrap Modal Window

I am using bootstrap 3. There is a select box inside the modal window. On change event of that select box i have to display its value at console. First time the value is correct but after that no value is shown on change event. Here is my code.
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
<h4 class="modal-title">Class</h4>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<select name='class' id='class'>
<option value='Class 1'>1</option>
<option value='Class 2'>2</option>
<option value='Class 3'>3</option>
<option value='Class 4'>4</option>
</select>
</div>
</form>
</div>
<script>
var class = $('#class');
class.on('change', function (e) {
console.log(this.value);
});
</script>
Can anyone help me out..where I am wrong.
Note: The code is working first time without any problem. But when I close the modal & open the modal again without page refresh then no value is shown at console. If page is refreshed then its working.
Here is the working fiddle for you.
Please avoid using reserved keyword as variable, class or id names.[https://jsfiddle.net/4s5mkbj8/2/]
$('#class').on('change', function (e) {
console.log(this.value);
});