Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

327
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!