link2329 link2330 link2331 link2332 link2333 link2334 link2335 link2336 link2337 link2338 link2339 link2340 link2341 link2342 link2343 link2344 link2345 link2346 link2347 link2348 link2349 link2350 link2351 link2352 link2353 link2354 link2355 link2356 link2357 link2358 link2359 link2360 link2361 link2362 link2363 link2364 link2365 link2366 link2367 link2368 link2369 link2370 link2371 link2372 link2373 link2374 link2375 link2376 link2377 link2378 link2379 link2380 link2381 link2382 link2383 link2384 link2385 link2386 link2387 link2388 link2389 link2390 link2391 link2392 link2393 link2394 link2395 link2396 link2397 link2398 link2399 link2400 link2401 link2402 link2403 link2404 link2405 link2406 link2407 link2408 link2409 link2410 link2411 link2412 link2413 link2414 link2415 link2416 link2417 link2418 link2419 link2420 link2421 link2422 link2423 link2424 link2425 link2426 link2427 link2428 link2429 link2430 link2431 link2432 link2433 link2434 link2435 link2436 link2437 link2438 link2439 link2440 link2441 link2442 link2443 link2444 link2445 link2446 link2447 link2448 link2449 link2450 link2451 link2452 link2453 link2454 link2455 link2456 link2457 link2458 link2459 link2460 link2461 link2462 link2463 link2464 link2465

[Vue.js] Vuex store getter always returning false

when new to vue.js js, and also with Vuex. when having trouble accessing store getters,

You can see in getter, Activated is true, And In My getter in store.js is

getters: {
loggedIn(state) {
return state.token !== null
},
Activated(state) {
return state.activated;
},

You can see state activated also is true,
But when I access by

store.getters.Activated

It always return false, but When I access another getter logged in by same method in same function it gives the value true.

store.getters.loggedIn

Where I tried to access this in

router.beforeEach((to, from, next) => {
if (to.matched.some(record => record.meta.requiresAuth)) {
if (!store.getters.loggedIn) {
next({
name: ‘home’,
})
} else {
next()
}
}
else if (to.matched.some(record => record.meta.requiresActivation)) {
debugger;
if (store.getters.Activated===true) {
next()
} else {
next({
name: ‘activate’,
})
}
}
else if (to.matched.some(record => record.meta.requiresVisitor)) {
if (store.getters.loggedIn) {
next({
name: ‘activate’,
})
} else {
next()
}
} else {
next()
}

})

mutation in store js

mutations: {
updateFilter(state, filter) {
state.filter = filter
},

retrieveToken(state, token) {
state.token = token
},
CheckActivation(state, value) {
state.activated = value
},
destroyToken(state) {
state.token = null
},

and actions

actions: {
CheckActivation(context){
if(this.token===null){
context.commit(‘CheckActivation’, false)
return false;
}
else{
var settings = {
“async”: true,
“crossDomain”: true,
“url”: “http://vuejs.test/api/user",
“method”: “GET”,
“headers”: {
“Accept”: “application/json”,
“Authorization”: “Bearer “+ this.state.token,
“cache-control”: “no-cache”
}
}
$.ajax(settings).done(function (response) {
if(response.verified)
context.commit(‘CheckActivation’, true)
else
context.commit(‘CheckActivation’, false)
});
}
},

Please Help.

Solution :

So it’s most probably because you would be accessing the store variable before it is set to true .
For accurate solution please add code of mutation of above variable and method call.May be you will need to use async await in actions of store and in method call of vue.js file.

[Vue.js] MVC form submission need query string with %20 not with '+'

there is a search input type in my form. When the form is submitted, the page reloads with query params with +

So if I search for abc def the query param becomes ?q=abc+def the url becomes https://localhost:44300/Search?q=abc+def

Now this is sent to the webapi as an api call. so then on server side when replacing the + with space then performing the search on the controller. Now when I search for abc def+efg it becomes abc+def%2Bdef

request.Query = request.Query.Replace(‘+’, ‘ ‘);
request.Query = HttpUtility.UrlDecode(request.Query);

So on server side when first replacing the param’s +s with a space character then decoding it and finally I get abc def+efg on the controller which is what I was looking for.

All this could be avoided if on the form submission the url was encoded. so if from beginning the query param was ?q=abc%20def%2Befg I would just need to decode it.

How to do that?

Edit

The page renders first, then a vue.js component gets the query parameter, and makes a call to api controller with axios. /api/search

const searchQuery = this.$router.getQueryParam(‘q’);

this.queryParams.query = searchQuery ? searchQuery : null;

return axios.get(`/api/search`, {
params: queryParams,
});

Solution :

If you were submitting a form, the urleconding would be automatically applied. Since you are getting and sending the search query without form submission, just apply a javascript function to encode that fetched value:

this.queryParams.query = searchQuery ? encodeURI(searchQuery): null;

Solution 2:

You can do like this.

Uri.EscapeUriString(request.Query)

[Vue.js] How to create global functions in Vue.js?

I would like to use some functions globally in my app.
Most of the answers for my question refer to vue.js Mixin. Although I use it, it cant solve my problem. This is my try

Vue.mixin({
methods:{
gFun(){
console.log(“Works”);
},
}
})

const app = new Vue({
el: ‘#app’,
data: {

},
created(){
gFun();
},
methods: {

}
});

vue.js says “ReferenceError: testMixin is not defined”.
is just to able to use gFun() globally(not using like this.gFun()). Please explain my something.
Using vue.js Mixin or not is ok.

Solution :

This is not a problem with Vue.

You can create global functions in javascript using something like:

window.gFun = window.gFun || () => console.log(“Works”);

Solution 2:

You could declare the globale function like function functon_name(parameters){…}, try to pass the vue.js instance like a parameter in order to access its properties inside the global function as follows

Vue.config.devtools = false;
Vue.config.productionTip = false;

window.gFun = function(vm) {
console.log(vm.message);
}

const app = new Vue({
el: ‘#app’,
data: {
message: “Hello !”
},
created() {
gFun(this);
},
methods: {

}
})
<link type=”text/css” rel=”stylesheet” href=”//unpkg.com/bootstrap/dist/css/bootstrap.min.css” />
<script src=”https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>

<div id=”app” class=”container”>

</div>

[Vue.js] Reactive binding with VueJS

I’m trying to bind a class like so:

:class=”{active: favs.medium_title.fontWeight === ‘bold’}”

Except that fontWeight doesn’t exist yet when the component is mounted.

Here’s my object:

favs: {

medium_title: {},

}

So when I add the fontWeight property and it’s value it doesn’t set the active class.

Solution :

You could use vm.$set (as you can read here) to add the new property:

this.$set( this.favs.medium_title , ‘fontWeight’ , ‘bold’ )

In this way it will be reactive and changes will be detected.

Solution 2:

You need to add fontWeight to the data object and give it a default value so fontWeight exists on mounted.

Solution 3:

Because the property fontWeight ain’t declared it is not reactive.Hence you will need to declare it in the declaration of object.

In this the class is binding properly on change of value in the property on button click.

<div id=”app”>
<p :class=”{active: favs.medium_title.fontWeight === ‘bold’}”>{ message }</p>
<button @click=”favs.medium_title.fontWeight = ‘bold’”>
Click here
</button>
</div>

new Vue({
el: ‘#app’,
data: {
message: ‘Hello Vue.js!’,
favs: {
medium_title: {
fontWeight:’’
},
}

}
})

Refer the below fiddle for the solution.
https://jsfiddle.net/xgh63uLo/

If u don’t want to declare the property in advance you may use Vue.$set to set the new property with making it reactive
Check below link
https://vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats

[Vue.js] Vuelidate date input

I’m using vuelidate in my vue.js app. to set validation for date minDate for one week from now and maxDate for 3 months from now.

<label class=”input__label” for=”account_date”>Wane do:</label>
<input type=”date”
class=”input__col”
id=”account_date”
v-model=”date”
@input=”$v.date.$touch”
required>

-

date: {
required,
//minDate
},

I can’t find anything about date in vuelidate docs.

Solution :

Unfortunately, there are no built-in validators for date in vuelidate:
https://monterail.github.io/vuelidate/#sub-builtin-validators

Anyway, you can use JS lib like luxon and create the custom validator to check dates.

Luxon has great documentation where you can find really helpful methods like plus or diff that can be handy in the case :)

[Vue.js] Format HTML in VSCode the same way as Visual Studio

I’m searching a way to configure VSCode to format HTML (and markup files in general, such as .vue) in the same way Visual Studio is doing.

In Visual Studio, for instance, if I write something like:

<div id=”testid” class=”test” style=”display: block;”>
<p class=”p1”></p>
</div>

I can decide where to break the attribute’s line, so if I insert a newline after the class I’ll have:

<div id=”testid” class=”test”
style=”display: block;”>
<p class=”p1”></p>
</div>

Or I can break the line after the id:

<div id=”testid”
class=”test” style=”display: block;”>
<p class=”p1”></p>
</div>

Etc…

The point is that Visual Studio is aware of the newline I insert, so if I strike CTRL+K, CTRL+D it will actually format my document, but will maintain the newline as I choose, and the attributes at the newline are just aligning to the first attribute in the line of the tag.

That’s something I cannot emulate in VSCode, I tried the builtin formatter, Prettier and Beautify…

Is there actually a way to do it?

EDIT

As @Mr_b194 suggested I tried HTML Format extension. Compared to Beautify this one is actually preserving the newlines between attributes, but the first attribute in the new line is just indented (4 chars in my case), resulting not aligned with the upper line:

<div id=”testid”
class=”test” style=”display: block;”>
<p class=”p1”></p>
</div>

Visual Studio is aligning the ‘class’ attribute with the ‘id’ of the line before.

Solution :

Go to the extension in Vs code and install html format. Press the reload button, this would help to format all the html code

Solution 2:

Ok, I found out a way!

I was using the version 1.4.7 of the Beautify extension and there was a schema misalignment for the intellisense of .jsbeautifyrc file that was not showing you all the options for the property wrap_attributes.

I opened an issue on github and the guys where super fast, they’ve already released the 1.4.8 that correctly shows the preserve-aligned value I was searching.

So, to make it short, you can configure the .jsbeautifyrc file for the project with:

{
“preserve_newlines”: true,
“wrap_attributes”: “preserve-aligned”
}

And is gonna work.

As I was searching also for .vue.js support, you can configure Vetur with:

“vetur.format.defaultFormatterOptions”: {
“js-beautify-html”: {
“preserve_newlines”: true,
“wrap_attributes”: “preserve-aligned”
}
}

And is going to work the same way.

[Vue.js] Vue CLI 3 prevent cache busting for certain output files

I’m building a chrome extension with vue.js cli 3. I’ve got the basics working well, but I was hoping to also run my content and background javascript through the build process instead of just putting them into my public folder and copying it into dist. This is mostly just so I can use import/export to clean up my file structure.

I was able to add them as new “pages” in the vue.js config and even without the html template file, they get built properly and moved into dist.

The problem is, they then get the cache busting string appended to their filename so I’m unable to reference them in the extension manifest. For example, background.js becomes background.d8f9c902.js

Is it possible to tell the vue.js config that certain “pages” should not get the cache busting? The documentation here does not seem to expose that as a parameter.

Thanks in advance!

Solution :

Filename hashing can be disabled for all files:
https://cli.vuejs.org/config/#filenamehashing

It works in my case using below vue.config.js:

// vue.config.js
module.exports = {
lintOnSave: true,
filenameHashing: false
}

[Vue.js] In vue js only one list is appearing from template

when in learning phase of vue.js and here is my index.html code as follows:

<html>
<head>
<link rel=”stylesheet” href=”index.css”>
<script src=”https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>

<div id=”app”>
<ul>
<todo-item></todo-item>
</ul>
</div>

<script src=”index.js”></script>
</body>
</html>

and code of index.js is as follows:

Vue.component(‘todo-item’, {
template: ‘<li>Todo 1</li><li>Todo 2</li>’
})

var app = new Vue({
el: ‘#app’
})

now in the output, when getting only one <li>Todo 1</li> not the second Todo 2.
Can anyone let me know why it is happening?

In case I repeat the <todo-item></todo-item> in index.html then it is repeating Todo 1, should not it display Todo 2 after Todo 1?

Solution :

there is gotten the answer, It was happening because

Component template should contain exactly one root element.

and I was using two <li> in the same template without a root element.

So there is changed in the template as

template: ‘<ul><li>This is a todo</li><li>Another work</li></ul>’

and removed <ul> from index.html. It is working fine now.

[Vue.js] Vue - Disable Eslint

I did some research, but could not find a quick fix to this situation:

Vue-cli 3.4.0 - Webpack 4.29.0

My project does not have config/index.js as mentioned here.
I tried to open vue.config.js and disable eslint does not work.
I removed the cli-plugin-eslint package.
Don’t have .eslintrc.js.
Could not find any other configuration about eslint in project.

-– Fix —

I realize that I didn’t control my settings well. It was built in vetur.

Solution :

the easiest way to ignore eslint is to ignore the files you dont want to be linted. For example, create .eslintignore in the project root folder. then just add the following folder which you dont want to lint like,
add src\

[Vue.js] Produce Random Numbers in Laravel with Vue.js

I’m new in Vue.js and I’m having a hard time producing random numbers. In my vue.js file, there is this piece of code under script:

var numeral = require(“numeral”);
Vue.filter(“formatNumber”, function(value)
{
return numeral(value).format(“0,0”);
});

…calling the method:

getRandomNum()
{
this.like = Math.random() * 100;
this.rate = Math.random() * 10;
this.comment = Math.random() * 100;
}

works like a charm that produces the number of likes, ratings and comments in my html:

However, if you’d notice, even though they’re random in a sense whenever I refresh my DOM, technically they’re still all the same in each rows. My question is, is there a way that I can produce a whole totally different random numbers in each row? Thanks! Please respect, I’m new to Vue.js.

Solution :

the reactive data properties are global, meaning they are defined in the global data scope

data(){
return {
likes: 0,
rate: 0,
comment: 0
}
}

you have to make each of the iterable elements (v-for loop) into a component. and pass the relevant data into it. When it is rendered, it would be done uniquely. For example.

<div v-for=”comment in comments”>
<comment-component :comment=”comments”></comment-component>
</div>

in the comment-component

<template id=”comment-component”>
<div id=”box”>
<h3>{comment.title}</h3>
<p>{comment.description</p>
<div>{comment.date} | {getRandomNumber()}</div>`
</template>
export default{
data(){
return {
likes: 0,
rate: 0,
comment: 0
}
},
methods: {
getRandomNum()
{
this.like = Math.random() * 100;
this.rate = Math.random() * 10;
this.comment = Math.random() * 100;
}
},
filters:{

}

Now when the page renders, it treats each item in the collection independently.