搜尋

首頁  >  問答  >  主體

javascript - vue 路由初學疑問

1:初學vue 對router-link 及 router-view這塊不是特別懂,大致問題如下:

現有程式碼結構如下,可以理解為左邊一個導航,右邊一塊顯示區域

//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
    }
] 

設定路由之後,點選router-link可以有效渲染到router-view中
但我本身想要的是以下結構

<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>

但這個時候點「發現音樂」 並沒有渲染至router-view,請問這個路由該怎麼改
這塊兒確實不太懂,哪位大神麻煩能給講解講解嗎?

PHP中文网PHP中文网2689 天前837

全部回覆(3)我來回復

  • 怪我咯

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

    router.js應該這樣寫吧
    //router.js
    Vue.use(Router)
    import foundMusic from "../compontents/found_music.vue"
    export default new Router({
    rou

    routes:[

    tes

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

    ]

    })

    你試試看 components:{ "left-menu":leftMenu }🎜

    回覆
    0
  • 高洛峰

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

    看下命名檢視 https://router.vuejs.org/zh-c...

    回覆
    0
  • typecho

    typecho2017-07-05 10:42:40

    我在重新對邏輯進行審查之後發現正常了,下面把正常的程式碼貼一遍吧,也算是幫助新人了。在此非常感謝回答問題的朋友

    //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>

    回覆
    0
  • 取消回覆