Сообщество IT-специалистов
Ответы на любые вопросы об IT
Профессиональное развитие в IT
Удаленная работа для IT-специалистов
// Вызов методов корневого экземпляра this.$root.baz()
methods: { this.$root.baz() }
methods: { method1() { this.$root.baz() } }
new Vue({ data() { return { ParamsAffiliate: '' } }, mounted() { this.getParams(); }, methods: { getParams(){ const ParamsAffiliate = localStorage.getItem("ParamsAffiliate"); if(ParamsAffiliate){ this.ParamsAffiliate = JSON.parse(ParamsAffiliate); return; } axios.get('/information/userbar/') .then(response => { this.ParamsAffiliate = response.data; localStorage.setItem('ParamsAffiliate', JSON.stringify(response.data)) }) } } });
ID: {{ParamsAffiliate.id}}
export default { name: 'IdWeb', data() { return { ParamsAffiliate: '' } }, mounted() { this.getParams() }, methods: { getParams(){ this.$root.getParams() } } }
import axios from 'axios'; export default new vuex.Store({ state: { params: null, }, mutations:{ setParams(state, payload){ state.params = payload; } }, actions: { async getParams({commit, state}){ let response = await axios.get('/information/userbar/'); if (response.status === 200) { commit('setParams', response.data); } } } });
component.vue
import { mapState } from 'vuex'; export default { name: 'IdWeb', computed: { ...mapState([ 'params' ]) }, mounted() { this.$store.dispatсh('getParams'); }, }
<template> <li class="state_account_id" v-if="params"> ID: {{params.id}} </li> </template>
async mounted() { await this.$store.dispatсh('getParams'); },
const ParamsAffiliate = localStorage.getItem("ParamsAffiliate"); if(ParamsAffiliate){ this.ParamsAffiliate = JSON.parse(ParamsAffiliate); return; } axios.get('/information/userbar/account') .then(response => { this.ParamsAffiliate = response.data; localStorage.setItem('ParamsAffiliate', JSON.stringify(response.data)) })
//Ввод localStorage.ParamsAffiliate=JSON.stringify(response.data) //Вывод ParamsAffiliate=localStorage.ParamsAffiliate