link685 link686 link687 link688 link689 link690 link691 link692 link693 link694 link695 link696 link697 link698 link699 link700 link701 link702 link703 link704 link705 link706 link707 link708 link709 link710 link711 link712 link713 link714 link715 link716 link717 link718 link719 link720 link721 link722 link723 link724 link725 link726 link727 link728 link729 link730 link731 link732 link733 link734 link735 link736 link737 link738 link739 link740 link741 link742 link743 link744 link745 link746 link747 link748 link749 link750 link751 link752 link753 link754 link755 link756 link757 link758 link759 link760 link761 link762 link763 link764 link765 link766 link767 link768 link769 link770 link771 link772 link773 link774 link775 link776 link777 link778 link779 link780 link781 link782 link783 link784 link785 link786 link787 link788 link789 link790 link791 link792 link793 link794 link795 link796 link797 link798 link799 link800 link801 link802 link803 link804 link805 link806 link807 link808 link809 link810 link811 link812 link813 link814 link815 link816 link817 link818 link819 link820 link821

[Vue.js] get article id by axios with vuex Subscribe to RSS

Trying to reach article’s id and fetch it. But in somewhere I do some mistakes. Because vuex returns empty object and router-link is fails.

so far, I made some something like below.

state: {
article: {},
},

getters: {
bringArticle(state){
return state.article;
}
},

mutations: {
updateArticle(state, article){
state.article = article;
}
},

actions: {
getArticle(context, id){
axios.get(“/api/articles/“ + id)
.then((response) => {
context.commit(“updateArticle”, response.data);
console.log(response.data)
})
.catch((error) => {
console.log(error);
})
}
}

Clearly this axios.get(“/api/articles/“ + id) is not working out…

by the way. I see id’s data at the endpoint without a problem. Just stuck the axios part. How can I reach the id?

Route::get(“/articles/{id}”, “Api\ArticlesController@singleArticle”);

Solution :

okay, it took some time to figure it out. And not sure it is the best way, but it’s working now.

I set the route-link like below to go to ID.

<router-link :to=”{ name: ‘detail’, params: { id: article.id} }”>

then I created api.js file and export my calls.

var apiUrl = “api/articles/“;

export default {

getArticles: function(){
return axios.get(apiUrl);
},

getArticle: function( id ){
return axios.get(apiUrl + id);
},

}

inside my store.js I changed my action commit like:

getArticle( { commit }, data ){
path.getArticle( data.id )
.then( function( response ){
commit( ‘setArticle’, response.data );
})
.catch( function(){
console.log(error)
});
},

Last, I dispatch it Detail.vue.js

created(){
this.$store.dispatch( ‘getArticle’, {
id: this.$route.params.id
});
},

[Vue.js] Using Vuex with dynamically created components Subscribe to RSS

to use Vuex to manage dynamically created components. In my app, there is multiple pages and “field” components on each page. Multiple field components can be added on each page. Each field component will have text fields that users can edit. I would like to keep track of those values as users type, so that state is always up-to-date (like v-model). Multiple pages can be added as well.

when having trouble finding a way for Vuex to keep track of the new pages and field components that users add or remove.

//example of how my state looks
const state = {
pages: [
{
id: ‘1’,
name: ‘Contact Information’,
fields: [
{
name: ‘First Name’,
type: ‘text-field’,
required: true
}
]
},
{
id: ‘2’,
name: ‘Activities’,
fields: [
{
name: ‘Summary’,
type: ‘textarea-field’,
required: true
}
]
}
]
}

//pages are just tabs
<a @click=”addPage”>Add Page</a>
<ul class=”nav nav-tabs” data-tabs=”tabs”>
<li class=”nav-item” v-for=”(page,key) in allPages” :key=”key”>
<a class=”nav-link” data-toggle=”tab”>
{page.name}
</a>
</li>
</ul>

//example of how the field html looks
<div class=”tab-pane” v-for=”(page, key) in allPages” :key=”key”>
<div v-for=”(field, key) in page.fields” :key=”key”>
<page-field :value=”field.name” :type=”field.type”/>
</div>
<a @click=”addField”>Add Field</a>
</div>

//field component example
<template>
<div>
<input type=”text”> //to keep track of this input with Vuex every time a new field component is added
</div>
</template>

Any time a new page or field component is added; or when someone types in the text field inside the field component, my state to be updated.

Solution :

You could add an input-event handler that invokes a Vuex mutation to set the page field…

In the store, update the fields array-element to contain a value property, which will hold the user input:

/* store.js */
const state = {
pages: [
{
// …
fields: [
{
// …
value: null
}
]
},
{
// …
fields: [
{
// …
value: null
}
]
}
]
}

Also add a Vuex mutation that sets the page field:

/* store.js */
const mutations = {
SET_PAGE_FIELD(state, { pageId, fieldName, fieldValue }) {
const page = state.pages.find(page => page.id === pageId)
if (page) {
const field = page.fields.find(field => field.name === fieldName)
if (field) {
field.value = fieldValue
}
}
}
}

export default new Vuex.Store({
state,
mutations
})

Update page-field’s input elements to emit an input event with the target value:

/* PageField.vue.js */
<template>
<div class=”page-field”>
<input @input=”$emit(‘input’, $event.target.value)” …>
<textarea @input=”$emit(‘input’, $event.target.value)” …/>
</div>
</template>

Handle the input event in the parent by mutating the page field in the store (via the mutation in step 2):

/* App.vue.js */
<template>
<div class=”tab-pane” v-for=”page in allPages” :key=”page.id”>
<div v-for=”field in page.fields” :key=”field.name”>
<page-field
@input=”setField(page.id, field.name, $event)”
:type=”field.type”
/>
</div>
</div>
</template>

<script>
export default {
methods: {
setField(pageId, fieldName, fieldValue) {
this.$store.commit(‘SET_PAGE_FIELD’, {
pageId,
fieldName,
fieldValue
});
}
}
}
</script>

[Vue.js] Vue3 autoprefixer config issue Subscribe to RSS

there is a new project that was created with Vue-cli 3 using vue.js create my-new-project. when using CSS grid for some layout things and i need to support IE11 and newer. vue.js docs say that autoprefixer is loaded and enabled by default but its not working. I cant get it to work in either npm run build or npm run serve. Works fine in chrome but IE11 its not working. Im sure there is some config that needs to be done but im unsure what that may be.

.browserslistrc:

\> 1%
last 4 versions

postcss.config.js:

module.exports = {
plugins: {
autoprefixer: {}
}
};

Solution :

CSS Grid support is disable by default.
You can enable it by using either the grid: autoplace option or the /* autoprefixer grid: autoplace */ control comment.

module.exports = {
plugins: {
‘autoprefixer’: {
grid: ‘autoplace’
},
}
};

Does Autoprefixer polyfill Grid Layout for IE?

[Vue.js] Vue-CLI importing global scss file Subscribe to RSS

I’m trying to populate an scss file for the Vue-cli project in App.vue.js which has been

<style lang=”scss”>
@import url(‘https://fonts.googleapis.com/css?family=Roboto:100,100i,300,300i,400,400i,500,500i|Rubik:300,400,500,700,700i,900');
@import “../node_modules/bootstrap/scss/bootstrap”;
@import “./assets/css/style”;
</style>

From node modules bootstrap SCSS work properly, but it’s not working when I kept style.scsson assets folder !

Solution :

Import from the main.js file, like this:

import ‘@/assets/css/style’;

[Vue.js] How to activate the history mode of the vue-router on a subrout of a website written in php, apache server? Subscribe to RSS

when including a SPA on my website in apache server, but when accessing the route of this, I load the views but the route returns to the main domain. How should I configure the router or .htaccess to correct this?

My website is like www.example.com , and the vue.js SPA is located in www.example.com/en/route1/route2/spa.

When I access this route, the vue.js SPA loads but the route returns to the main domain www.example.com, and the vue.js SPA routes loads as usual but in the main domain route, like www.example.com/spa/spaRoute1.

What is to preserve the original route of the vue.js SPA, to shows the SPA routes from there, like www.example.com/en/route1/route2/spa/spaRoute1 using the history mode of Vue-Router.

there is tried modify the .htacces like the documentation of vue-router shows but it doesnt help.

https://router.vuejs.org/guide/essentials/history-mode.html#example-server-configurations

there is the router in history mode like this:

export default new Router({
mode: ‘history’,
etc….
})

and my .htaccess using the configuration of vue-router docs

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Solution :

you tried using the base property ? https://router.vuejs.org/api/#base

[Vue.js] NPM run serve is showing old state of my vuejs project on localhost Subscribe to RSS

npm run serve is showing an old state of my vue-cli project on localhost but not the actual one on the network address

there is tried to restart the npm run serve command but it didn’t help

This is the output of the command

DONE Compiled successfully in 46105ms 5:31:39 PM

App running at:
- Local: http://localhost:8080/
- Network: http://“network ip address”:8080/

Note that the development build is not optimized.
To create a production build, run npm run build.

I expected it to show the current version of my vue.js project that I set up with vue.js cli but it’s showing an old state of it

Solution :

1 - Clear cache of the browser, try Chrome on incognito.

2 - Check where are the dist folder and if the code inside are updated (put console.log(“testing”) and search in the folder)

3 - Delete dist folder and run again npm run build

4 - Search the version in all the proyect to ensure where is the version get to show on web.

Hope it helps :)

[Vue.js] img srcset in vue-cli project results in compile error Subscribe to RSS

there is an image for a vue-cli project, and it works fine with the following path:

<img src=”../assets/images/banner/hero_2x_w4eyw5_c_scale,w_2086.jpg” alt=””>

But when I insert image URLs with srcset for responsive image breakpoints, I see a compile error.

Here is the generated image code:

<img sizes=”(max-width: 3840px) 100vw, 3840px”
srcset=”../assets/images/banner/hero_2x_w4eyw5_c_scale,w_320.jpg
320w,../assets/images/banner/hero_2x_w4eyw5_c_scale,w_1099.jpg
1099w,../assets/images/banner/hero_2x_w4eyw5_c_scale,w_1647.jpg
1647w,../assets/images/banner/hero_2x_w4eyw5_c_scale,w_2086.jpg
2086w,../assets/images/banner/hero_2x_w4eyw5_c_scale,w_2460.jpg
2460w,../assets/images/banner/hero_2x_w4eyw5_c_scale,w_2848.jpg 2848w,
../assets/images/banner/hero_2x_w4eyw5_c_scale,w_3214.jpg
3214w,../assets/images/banner/hero_2x_w4eyw5_c_scale,w_3556.jpg
3556w,../assets/images/banner/hero_2x_w4eyw5_c_scale,w_3805.jpg
3805w,../assets/images/banner/hero_2x_w4eyw5_c_scale,w_3840.jpg 3840w”
src=”../assets/images/banner/hero_2x_w4eyw5_c_scale,w_3840.jpg” alt=””>

Error!

Solution :

The error occurs because vue-loader does not handle commas in srcset filenames/URLs. It simply splits the srcset string by ,, so the first srcset value of ../assets/images/banner/hero_2x_w4eyw5_c_scale,w_320.jpg is parsed as ../assets/images/banner/hero_2x_w4eyw5_c_scale, leading to the Module not found error in the screenshot.

MDN docs indicate that the srcset values are comma-delimited, and it’s unclear whether the commas are permitted within the filenames/URLs. In any case, you could resolve the build errors by renaming the files to remove the commas, and updating the references in the srcset to match.

[Vue.js] Unable to use this.$router.push(' ') inside docRef function Subscribe to RSS

when trying to use vue-router in my quasar project. this.$router.push( ‘ ‘ ) works everywhere on the page except inside this docRef block.

I found out that this.$router.push still works in the same component.

This router call works:

const userRef = this.$db.collection(‘users’).doc(this.$fb.auth().currentUser.uid)
console.log(‘the id is’ + this.$fb.auth().currentUser.uid)
userRef.set({
accountUID: this.$fb.auth().currentUser.uid,
accountChosen: true,
accountType: ‘user’
})
this.$router.push(‘../user/home’)

This does not work (in the same component):

var docRef = this.$db.collection(‘users’).doc(uid)
docRef.get().then(function (doc) {
data = doc.data().accountType
if (data === ‘user’) {
console.log(‘account is users’)
console.log(data)
this.$router.push(‘./user/home’)
} else if (data === ‘truck’) {
console.log(‘account is a truck’)
this.$router.push(‘./truck/home’)
} else {
console.log(‘in else statement’)
}
}).catch(function (error) {
console.log(error)
})

Chrome Console Error

TypeError: Cannot read property ‘$router’ of undefined
at eval (Check.vue?a4ce:44)

Solution :

Instead of:

docRef.get().then(function (doc) {

Use:

docRef.get().then((doc) => {

Explanation

The this inside an arrow function body block refers to the current context, in the case, the Vue/component instance.

But when you use function instead of an arrow function, the {} block creates a new context, changing the semantics of this, making it point to something else than the this outside that function.

In other words, the problem happens because each function () {} has its own context (its own this), which could be set to something else. Arrow functions, OTOH, inherit the context (the this) of where it is declared. In this case, since you are declaring it inside a method, the this is the vue.js instance. Using an arrow function keeps it. Using a function() does not guarantee it (the this could be set to something else, which generally is the case).

For futher details, I recommend MDN - Arrow Functions.

[Vue.js] Libsignal JavaScript library Subscribe to RSS

What is the best way to include the libsignal JavaScript library in my vue.js project. I tried adding it as a script in the main.js file but when I use the keyword libsignal in my components it raises an undefined error.

Solution :

For vue.js project you can install libraries by npm.

For installing libsignal, refer libsignal package link.

Run this command in cmd in the project folder.

npm i libsignal

Hope this helps!

[Vue.js] Why the php output can not insert into Vue data array Subscribe to RSS

when learning basic Vue.js, when I fetch data using PHP, why it can not put into the vue.js object data array

data: {
message: “vue?”,
homeView: true,
brandView: false,
flipF: true,
flipB: false,
prices:[
{coffee1:15},{coffee1:12},
<?php
while($row = mysqli_fetch_array($result)){
echo “{coffee1:”.$row[‘price’].”},”;
}
?>
]
},

{ item.coffee1}

The php part could output {coffee1:20},{coffee1:18}, but they can not be shown on webpage

Solution :

Ideally, you should fetch from an API which will serve JSON, but for sake of answering you should instead build the array, and then use json_encode().

<?php
$prices = [
[‘coffee1’ => 15],
[‘coffee1’ => 12]
];

while($row = mysqli_fetch_array($result))
$prices[] = [‘coffee1’ => $row[‘price’]];

?>

Now in the vue:

data() {
return {
message: “vue?”,
homeView: true,
brandView: false,
flipF: true,
flipB: false,
prices: <?= json_encode($prices) ?>
}
},