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

186
Vistas
Pass data from plain javascript to vue

I have an app with plain JS and Vue in one file. I need to pass a variable value from JS to Vue. 0. All code in one file:

<script>
plain js
var plainJS = 100;
</script>
<script>
 var app = new Vue({
        el: '#vue-app',
....
</script>
  1. main functionality of the app is on plain js. Vue does a small part with UI.

  2. with js I can catch if one of my elements changed position (a dot on the screen)

  3. I need fire popup(some alert) if checkBox is selected but the Dot wasn't moved.

  4. checkBox is a Vue element

  5. I can pass data from Django to Vue

     this.vueVar = {{ djangoVar|safe }}
    

So how to pass

*var plainJS = 100;*

to vue app from plain JS part of the code?

Can you give me a simple way to set vueVar = plainJS?

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

0

This is one of the ways you can send data from js to js file. In your mainJS function popup you create a receive function that listens for change on storge.

$(window).on('storage', event => this.message_receive(this)(event));
  message_receive(self) {
    return function insideFun(ev) {
      if (ev.originalEvent.key !== 'checkboxFromVue') return;
      var message = JSON.parse(ev.originalEvent.newValue);
      if (!message) return;
      // Handle acction when checkbox is clicked
    };
  }

And in your Vue you use localstorage set item with key "checkboxFromVue"

localStorage.setItem('checkboxFromVue', valueOfCheckBox));

What this will do is whenever your checkbox changes localstorge value your main js known and receive that value

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can access a ref on the root component if you store a variable of what createApp returns. Then each time you would update your plainJS var, also reassign a matching property (ref) on the "app" object. For the initial value you may use a "root prop" which is the 2nd param of the createApp function.

main.js

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

var plainJS = 100;

const myApp = createApp(App, { plainJS: plainJS }).mount("#app"); 

setInterval(() => {
  //interval used here to simulate a value that changes at arbitrary times
  plainJS++;
  myApp.varFromOutsideVue = plainJS; // 👀 this updates the ref
}, 500);

App.vue

<template>
  <h1>{{ varFromOutsideVue }}</h1>
</template>

<script>
import { onMounted, onUnmounted, ref } from "vue";

export default {
  name: "App",
  props: {
    plainJS: { type: Number },
  },
  setup(props) {
    const varFromOutsideVue = ref(props.plainJS);

    return {
      varFromOutsideVue,
    };
  },
};
</script>

https://codesandbox.io/s/eager-rubin-6fv7p7?file=/src/main.js

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