Home / Snippets / Vue Js Fetch and Display HTML from Server Using Ajax
Vue Js Fetch and Display HTML from Server Using Ajax cover

Vue Js Fetch and Display HTML from Server Using Ajax

756

3 years ago

0 comments

In this short snippet, you will learn how to fetch HTML from the backend server and display them right away in the Vue Js component. The implementation is very simple and it's as follows.
<template>
  <html-fetcher url="/blog/static-content/some-post.html"/>
</template>

<script>
import axios from 'axios'

Vue.component('html-fetcher', {
  template: '<div>Loading…</div>',

  props: ['url'],

  mounted () {
    axios.get(this.url).then(({ data }) => this.$el.outerHTML = data)
  }
})
</script>
Do note that you will need to have Axios HTTP library installed to perform the ajax. If you have not done so, do run the command below.
npm install axios
If you are using Yarn then below is the command.
yarn add axios
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this