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

328
Vistas
How to point to an external javascript file with an array in vue?

I have this example below, where I'm using dropdowns to filter through data. As you can see, I have the [items] hard-coded into javascript file. I'm hoping to point to an external javascript file, and hoping someone can help me with the best way to do that:

data: {
selectedType: '',
selectedCountry: '',
selectedYear: '',
items: [{
    name: 'Nolan',
    type: 'mercedes, ford',
    year: '2020',
    country: 'england'
  },
  {
    name: 'Edgar',
    type: 'bmw',
    year: '2020',
    country: 'belgium'
  },
  {
    name: 'John',
    type: 'bmw, audi',
    year: '2019',
    country: 'england'
  },
  {
    name: 'Axel',
    type: 'mercedes',
    year: '2020',
    country: 'england'
  }
],

},

https://jsfiddle.net/yrbs2650/1/

Thanks in advance!

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

0

The question is a bit unclear around 'external' javascript file, I'm assuming you meant it will still live in the source code but in a different file.

Use an import statement in your Vue file and an export statement in your js file

items.js

const items = [
  {
    name: 'Nolan',
    type: 'mercedes, ford',
    year: '2020',
    country: 'england'
  },
  {
    name: 'Edgar',
    type: 'bmw',
    year: '2020',
    country: 'belgium'
  },
  {
    name: 'John',
    type: 'bmw, audi',
    year: '2019',
    country: 'england'
  },
  {
    name: 'Axel',
    type: 'mercedes',
    year: '2020',
    country: 'england'
  }
];

export default items;

app.vue

import items from './items.js';

new Vue({
  el: '#app',
  data: {
    selectedType: '',
    selectedCountry: '',
    selectedYear: '',
    items: items,
  }
  ...

Depending on where you put your items.js file will change the import path, if it is a neighbor to your app.vue then ./items.js will do the trick.

The JSON format is preferred for storing data like this. Read about it here

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