3

I use VueJs with VueCli, and I would like to load a CSS file according to a "source" parameter that would be passed to the initiation of my main App component.

I would like to initialize my component like this in the index file: Main.js:

import Vue from 'vue'
import App from './App'
import router from './router'
import VueLadda from 'vue-ladda'
import VueResource from 'vue-resource'
import VModal from 'vue-js-modal'
import BootstrapVue from 'bootstrap-vue'

Vue.use(VModal);
Vue.use(BootstrapVue);

Vue.use(VueResource);
Vue.config.productionTip = false
Vue.component('vue-ladda', VueLadda)

new Vue({
  el: '#app',
  props: ['source'],
  router,
  render: h => h(App)
})

Here my App component (App.vue)

<script>
export default {
  name: 'App',
  props: ['source'],
  data: function () {
    return {
    }
  },
  mounted: function () {
    console.log(this.source)
  }
}
</script>

But I get undifined. Someone would have an idea why?

2
  • how do you specify source parameter? What value do you want to see in console? Commented Jun 8, 2018 at 9:01
  • In the index.html <div id="app" source="foo"></div> In the console i will hop to see "foo" Commented Jun 8, 2018 at 10:04

3 Answers 3

7

You are not passing props on the root component, you are registering a prop name source.

You can pass props in the render function's createElement alias h function itself

new Vue({
  el: '#app',
  props: ['source'],
  router,
  render: h => h(App, {props:{ source:' source prop'}})
})

Or you can replace the render option with a template option to pass props to App.vue

new Vue({
  el: '#app',
  props: ['source'],
  router,
  components:{App},
  template: '<App source="source prop"/>'
})
Sign up to request clarification or add additional context in comments.

3 Comments

But in this case, it does not seem to be dynamic? I wish I could instantiate my component App with different sources. Once with 'foo' and another with 'bar' Something like that <div id="app" source="foo"></div> OR <div id="app" source="bar"></div>
then use dynamic binding using v-bind
Purely awesome... Thank you! It's the second answer which mentions the same quite powerful yet seem undocumented feature: stackoverflow.com/a/50854274/5113030 (Passing props to root instances in Vue.js...) It was quite time-consuming to find these two... A few alternatives commented there just in case: filosophy.org/code/… || v3-migration.vuejs.org/breaking-changes/props-data.html || lmiller1990.github.io/vue-testing-handbook/…
0

The template in App.vue is simple like this:

<template>
  <div id="app" source="mySource">
          <router-view/>
  </div>
</template>

Comments

0

If I understand you properly, this sounds like a bad idea. Props are for creating a reactive pipe to dynamic data, to update the dom. Links to scripts and stylesheets are not like other dom content, and loading css dynamically requires special treatment. You can't just change the href on the fly.

Why do you want to do this? There has to be a cleaner way.

2 Comments

In fact, I have to create a component that will be used by several different providers. All the logic will be the same but for each provider I must be able to use a different stylesheet. They will just have to include my built file as well as the div with the 'source' parameter. So at the pretender 'foo' he will have to insert my component with source = 'foo' to load the good style sheet
Indeed, once the source parameter is recovered, I will load the CSS once the component will be mounted with the technique mentioned in the previous link

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.