1

I use vue.js and vue-router.js. I added both of files to a html page

This is my component for load template... const Dashboard = {template: "<strong>"}

But I want load html page dynamically and with HTTP URL. const Dashboard = {template: "How load html page with url(http)"}

Can anyone guide me?

3 Answers 3

2
+50

you could define a component similar to this

Vue.component('dynamic-component', {
    props: {
        contentURL: String,
    },
    template: `<div v-html='inner_html'></div>`,
    computed: {
        inner_html() {
            return // http call to get html value utilizing this.contentURL
        },
    },
});

then from the router

{
    path: '/path/option-a',
    components: {
        DynamicComponent
    },
    props: {
        contentURL: `https://some-resource/option-a`
    }
},
{
    path: '/path/option-b',
    components: {
        DynamicComponent
    },
    props: {
        contentURL: `https://some-resource/option-b`
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, it seems the right way. I had expected to find it in some of the webpages for Vue resources, examples, components, etc... Pity there is not a Vue "stdlib"
btw... axios.get() is always a Promise, isn't it?
@arivero yeah, I was just stubbing in the http call, I don't know that OP uses axios so I use removed that
1

For creating layers, you can create vue component and use slot

Layout.vue

<template>
  Header
  AnyComponents
  <slot />
  Footer
</template>

and in your page component you should import your layout and wrap

MainPage.vue

<template>
  <MainLayout>
    Here is your conntent of page or router
    <router-view />
  </MainLayout>
</template>

<script>
import MainLayout from 'layout.vue';

export default {
  components: {
    MainLayout
  }
}
</script>

And now for render your page you should use your page in router

example of router.js

import Vue from 'vue';
import Router from 'vue-router'

import MainPage from 'MainPage.vue';

Vue.use(Router)

export default new Router({
  routes: [{
    path: '/',
    component: MainPage,
  }]
})

Comments

0

If you want to insert html to your page, use v-html

<div class="content" v-html="dashboard.template">
</div>

1 Comment

No ... I want make a layout and load html templates in <router-view></router-view> by url

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.