Hédi Ben Chiboub
Hédi Ben Chiboub

Reputation: 145

Prevent JSON file from becoming part of chunk.js/app.js when I build my vue app

I have a JSON file that I included in my vue app. But the problem is that when I build the app, the JSON file disappears and becomes a part from a js file. How can I keep it as a JSON file after build, since I want to manipulate it?

Upvotes: 1

Views: 235

Answers (2)

Negar
Negar

Reputation: 669

If you are using Vue CLI 3, you can place your static files in public directory and reference them by absolute paths.

For more information you can visit the CLI documents.


Update

  1. Place your JSON file in public directory
  2. Import axios or any similar tools you use for AJAX calls.
  3. You need a .env file in your project root. And store your base url in BASE_URL variable. (Read More!)
  4. Add a data property containing your base URL:

    data() {
        return {
            baseUrl: process.env.BASE_URL,
        }
    },
    
  5. And then you can access you JSON value with an ajax call:

        axios.get(this.baseUrl + 'test.json').then(response => {
            console.log(response);
        })
    

Upvotes: 1

Roy J
Roy J

Reputation: 43899

I think you want to configure the externals option.

Upvotes: 1

Related Questions