Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use vue-router in child component only?

I am new to vuejs and have some questions when using it. I create a root view, with 2 child component in it, the rootview.vue file looks like this:

<template>
  <div id="rootView">
    <calendar-top-view></calendar-top-view>
    <calendar-bottom-view></calendar-bottom-view>
  </div>
</template>

<script>
  import calendarTopView from './calendarTopView.vue'
  import calendarBottomView from './calendarBottomView.vue'
  export default {
  components: {
    'calendar-top-view': calendarTopView,
    'calendar-bottom-view': calendarBottomView
  }
}
</script>

and in calendar-bottom-view component, I need to use vue-router to help switch between to child components: calendarView and agendaView. So I decide to add vue-router just in my calendar-bottom-vue component, and the code looks like below:

<template>
  <div id='mainRightDiv'>
    <div id='calendarToolboxDiv'>
      <a v-link='{ path: "/calenarView"}'></a>
      <a v-link='{ path: "/agendaView"}'></a>
    </div>
    <router-view></router-view>
  </div>
</template>

<script type="text/javascript">
  import Vue from 'vue'
  import calendarView from './calendarView.vue'
  import agendaView from './agendaView.vue'
  import VueRouter from 'vue-router'
  Vue.use(VueRouter)
  let App = Vue.extend({})
  let router = new VueRouter()
  router.map({
    '/calendarView': {
      component: calendarView
    },
    '/agendaView': {
      component: agendaView
    }
  })
  export default {
    ready: function () {
      router.go({ path: '/calendarView'})
    }
  }
</script>

What I intend to do is to let the router to render calendarView first when the component is initialized. But when I run this page, I got error message like

'router-view> can only be used inside a router-enabled app.'

I checked the official spec of vue-router and found in its demo code, vue-router was used in entry js of the demo app, not found any use in any child component.

So I wonder, how can I use vue-router in one of my child component, instead of root?

like image 504
ericchan1336 Avatar asked Jul 07 '16 02:07

ericchan1336


People also ask

How do you access a root component from a child instance in Vue?

We can access the root instance of from a child component with the $root property. The code above created a rootFoo computed property from the root Vue instance's foo component and displayed it inside the foo component. So we get foo displayed.

How do I send data to child component Vue?

The way it works is that you define your data on the parent component and give it a value, then you go to the child component that needs that data and pass the value to a prop attribute so the data becomes a property in the child component. You can use the root component (App.


1 Answers

Assuming you use Browserify with Vueify, you create .js file, which should have structure:

var Vue = require('vue');
var Router = require('vue-router');
Vue.use(Router);
Vue.config.debug = true;
//any other npm packages

import Alert from './components/Alert.vue';
import SubVue1 from './pages/Subvue1.vue';
import SubVue2 from './pages/Subvue2.vue';
import Bar from './pages/Bar.vue';
import Baz from './pages/Baz.vue';

var App = Vue.extend({
    data: function () {
        return {
            //any ViewModel data
        }
    },
    components: {Alert}
});

var router = new Router();
router.map({
    // Not found handler
    '*': {
        component: {
            template: '<div class="text-align:center">' +
            '<h1>Not Found</h1>' +
            '</div>'
        }
    },
    '/sub1': {
        name: 'subvue1',
        component: SubVue1
    },
    '/sub2': {
        name: 'subvue2',
        component: SubVue2
        // add a subRoutes map under /foo
        subRoutes: {
             '/bar': {
               // Bar will be rendered inside Foo's <router-view>
               // when /foo/bar is matched
               component: Bar
             },
             '/baz': {
                // Same for Baz, but only when /foo/baz is matched
                component: Baz
             }
        }
   }
});

router.start(App, 'body'); // #app,.start -as you wish

Note that I use vue-router in the root parent .js file.So basically when you enter /sub1, then Subvue1.vue is loaded. Pay attention to subroutes (calendarView and agendaView switch). Every SubVue,Bar,Baz should have structure:

<template>
  <router-view><router-view> //For Subvue2 subroute
</template>
<script>
    export default {
        props: ['parentProperty'],
        data: function () {// data}
        methods:{}
    }
<style></style>

Read Vue Router docs for more info.

like image 162
Dandruff Avatar answered Sep 21 '22 00:09

Dandruff