简体   繁体   中英

Need an example vue-router (vue 2.x) for a Layout.vue with other route based components

I cannot figure out how to set this up properly using vue-router with vue.js 2.x

I want App.vue to be a main site layout module which contains basic site level things like footer, logo, main nav, etc.

A route based architecture which will load components based on the route inside this App.vue

ie: /things to show list and /things/:id to show individual item

I'm using the webpack template from vue-cli

I'm confused about main.js vs. App.vue should I be moving the routes out of main.js into App.vue?

Can someone link to a simple hello world using layouts in vue-router?

import Vue from 'vue'
import App from './App'
import Items from './components/Items'
import Item from './components/Item'
import Axios from 'axios'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
Vue.prototype.$http = Axios

const routers = new VueRouter([
  { path: '/items/:id', component: Item },
  { path: '/', component: Items }
])

// how to specify App.vue as a layout?
new Vue({
  routes
}).$mount('#app')

I think this should work for you

const app = new Vue({
    router,
    render: (h) => h(App)
}).$mount('#app')

or spread operator

const app = new Vue({
    router,
    ...App
}).$mount('#app')

As I mentioned in comment, take look at the repo that I created https://github.com/bedakb/vuewp/tree/master/public/app/themes/vuewp/app

I had wrong property name:

new Vue({
  router,
  ...App
}).$mount('#app')

This fixes it. I had imported it as routes not router . Another way to fix would have been { router: routes } but I renamed the file to router and now everything works.

Big thanks to @belmin for hoping on screenhero to try and help me fix it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM