Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
How would I add data on a button press to the either the root or a component of Vue 3?

My main problem is that I'm trying to reactively add data to either the Vue root or a Vue component. The reason this is an issue is because once my Vue app instance is mounted, (using app.mount()), I am unable to reactively add data to the Vue application. I am trying to use Vue along with vanilla JS as I am a novice with the framework. I guess what it really comes down to is... is there any event I could trigger or object I could call in vanilla JS to insert new data into lets say an array of objects within the Vue app instance?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Easiest way is to install Vuex into your vue 3 app and inject data through vuex store.

Here is simple, plain vue3 project generated from vue-cli with command vue create my-vue-prj

{
  "name": "my-vue-prj",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "core-js": "^3.6.5",
    "vue": "^3.0.0",
    "vuex": "^4.0.0-0"
  },
  "devDependencies": {
    ...((skipped))...
  }
}

And src/main.js entry point.

import { createApp } from "vue";
import App from "./App.vue";
import store from "./store";

const app = createApp(App);
app.use(store).mount("#app");

window.vueApp = app;
// or can expose store directly
// window.store = store; 
  • expose vue app instance(window.vueApp) to access from external javascript.

Vuex store can be access like this.

// external.js
const store = window.vueApp.config.globalProperties.$store
/*
 * can inject(modify, delete, etc) data into vue app through `store`
 */

Demo

An array is defined in vuex store like this

// file : src/store/index.js
import { createStore } from "vuex";

export default createStore({
  state: {
    nums: [0, 1, 2],
  },
  mutations: {
    replaceNum(state, nums) {
      state.nums = nums;
    },
  },
  actions: {},
  modules: {},
});
  • Array nums want to be render in App.vue

Array nums can be accessed from component App.vue.

<template>
  <img alt="Vue logo" src="./assets/logo.png" />
  <ul>
    <li v-for="(n, index) in $store.state.nums" :key="index">{{ n }}</li>
  </ul>
</template>
  • $store.state.nums is array of [0, 1, 2]
  • li is rendered with text (0, 1, 2)

Array nums can be updated from external js

// external.js
const store = window.vueApp.config.globalProperties.$store
store.commit('replaceNum', [2, 3, 5, 7, 11]);
  • commit('replaceNum', ...) - call method replaceNum in mutations. It updates nums and contents is also refreshed.
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda