search

Home  >  Q&A  >  body text

javascript - vue routing beginner questions

1: I am new to Vue and I don’t know much about router-link and router-view. The general questions are as follows:

The existing code structure is as follows, which can be understood as a navigation on the left and a display area on the right

//content.vue
<p class = "row">
    <p id="music_left_menu" class = "col-md-2">
        <h4>推荐</h4>
        <ul class="list-group">
            <li><i class = "fa fa-music"></i><router-link to = "/foundMusic">发现音乐</router-link> </li>
            <li><i class = "fa fa-bullhorn"></i> 私人FM</li>
            <li><i class = "fa fa-youtube-play" ></i> MV</li>
        </ul>
     </p>
     <router-view class="router-view col-md-10"></router-view>
</p>

//router.js
import foundMusic from "../compontents/found_music.vue"
routes:[
    {
        path:"/foundMusic",
        component: foundMusic
    }
] 

After configuring the route, clicking the router-link can effectively render it into the router-view
But what I want is the following structure

<p class = "row">
    <left-menu></left-menu>
     <router-view class="router-view col-md-10"></router-view>
</p>

import leftMenu from './menu_content/left_menu.vue'
export default {
  name: 'musicContent',
  components:{
      leftMenu
  }
}
//left_menu.vue 中的结构如下
<template>
  <p id="music_left_menu" class = "col-md-2">
    <h4>推荐</h4>
    <ul class="list-group">
        <li><i class = "fa fa-music"></i><router-link to = "/foundMusic">发现音乐</router-link> </li>
        <li><i class = "fa fa-bullhorn"></i> 私人FM</li>
        <li><i class = "fa fa-youtube-play" ></i> MV</li>
    </ul>
  </p>
</template>

But at this time, clicking "Discover Music" does not render to the router-view. How should I change this route?
I really don't understand this. Can any expert please explain it?

PHP中文网PHP中文网2698 days ago847

reply all(3)I'll reply

  • 怪我咯

    怪我咯2017-07-05 10:42:40

    router.js should be written like this
    //router.js
    Vue.use(Router)
    import foundMusic from "../components/found_music.vue"
    export default new Router({
    routes:[

    {
        path:"/foundMusic",
        component: foundMusic
    }

    ]

    })

    You try components:{ "left-menu":leftMenu }

    reply
    0
  • 高洛峰

    高洛峰2017-07-05 10:42:40

    Look at the named view https://router.vuejs.org/zh-c...

    reply
    0
  • typecho

    typecho2017-07-05 10:42:40

    After re-examining the logic, I found that it is normal. I will post the normal code below, which can be regarded as helping newcomers. Thank you very much to those who answered the question

    //App.vue
    <template>
      <p id = "app">
          <music-nav></music-nav>
          <music-content></music-content>
          <music-playing-wapper></music-playing-wapper>
      </p>
    </template>
    <script>
        import musicNav from "./nav.vue"
        import musicContent from "./music_content.vue"
        import musicPlayingWapper from "./playing_wapper.vue"
        export default {
            name:'app',
            components:{
                musicNav,
                musicContent,
                musicPlayingWapper
            }
        }
    </script>
    //main.js
    import Vue from 'vue'
    import App from './App.vue'
    import VueRouter from 'vue-router'
    import axios from 'axios'
    import router from './router/router'
    
    Vue.prototype.axios = axios
    Vue.prototype.MUSICAPI = '/api'
    
    Vue.use(VueRouter)
    new Vue({
      el: '#app',
      router,
      render: h => h(App)
    })
    //router.js
    import Router from "vue-router"
    import content from "../music_content.vue"
    import foundMusic from "../compontents/found_music.vue"
    import App from "../compontents/found_music.vue"
    
    export default new Router({
        routes:[
            {
                path:"/foundMusic",
                component: foundMusic
            }
        ] 
    });
    //music_content.vue
    <template>
      <p id="music_content" class = "container-fluid">
          <p class = "row">
              <left-menu></left-menu>
              <router-view class="router-view col-md-10"></router-view>
          </p>
      </p>
    </template>
    
    <script>
    import leftMenu from './menu_content/left_menu.vue'
    export default {
      name: 'musicContent',
      components:{
          leftMenu
      }
    }
    </script>

    reply
    0
  • Cancelreply