2

So I'm trying to wrap my head around how to use the vue-loader for Webpack, vue-router for routes, and how to have a shared data model. My understanding is that you create a data model, and then pass it into each component with the idea that they have a shared state. It also seems that you should use the data model rather than props, as that allows for two-way binding.

I've tried a few things but nothing seems to work. I tried passing the data through in the router, but that doesn't fix it.

I have this as my main app.js:

var $ = require('jquery'),
    Vue = require('vue'),
    VueRouter = require('vue-router');

Vue.use(VueRouter);

var first = require('vue!./first.vue');
var second = require('vue!./second.vue');

var sourceOfTruth = {
    msg: "my message"
};

var App = new Vue({
  data: sourceOfTruth,
});

var router = new VueRouter({
    history: true
});

router.map({
    '/foo': {
        data: sourceOfTruth,
        component: first
    },
    '/bar': {
        data: sourceOfTruth,
        component: second
    }
});

router.start(App, '#app');

And this as my first.vue:

<template>
    The message goes here -> {{ msg }}
</template>

<script type="text/javascript">
    // ???
</script>

Any ideas? Thanks!

4
  • require('vue!./second.vue'); Mind to explain what it does? I mean the part before the dot. Commented Jan 5, 2016 at 11:55
  • 1
    @YauheniPrakopchyk I assume thats how you require things using webpack. Commented Jan 5, 2016 at 13:48
  • @notANerdDev thanks for explanation. Commented Jan 5, 2016 at 14:07
  • @notANerdDev is correct - it forces webpack to load the file using vue-loader. Commented Jan 5, 2016 at 19:26

2 Answers 2

3

Check out this very good article which deals with communication between independent components. It is also an introduction to the Flux way of thinking with Vuex. Generally speaking you should be ok with a shared store instance, where you import it it every component from a separate file and make it reactive by assigning to each component's data.

<template>
    {{ sourceOfTruth.msg }}
</template>

<script>
    import store from '../store'

    export default {
       data () {
          return {
            sourceOfTruth : store.sourceOfTruth 
         }
      } 
   }
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That article is really helpful, and Vuex seems to be very promising.
1

Custom data fields that are sent through are not accessed like component data fields. You are trying {{ msg }}, this would look for a msg field in your component.

To access the data sent via router,

{{ $route.data }}

Now, since in your case, data is in turn json, you would do

{{ $route.data.msg }}

Comments

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.