Vue Router基礎(chǔ)用法(1)

一、步驟
  1. 在src目錄下新建router文件夾,并添加index.js文件
  2. router/index.js代碼如下
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

//定義路由
const routes = [
    {
        path: '/key1',
        component: () => import("../components/HelloWorld")   
    }
]

//創(chuàng)建router實(shí)例
const router = new VueRouter({
    routes      //相當(dāng)于routes: routes
    //...還可以配置其他參數(shù)
})


export default router
  1. 在main.js里面將router注入


    image.png

現(xiàn)在我們就可以通過this.$router來訪問路由器,也可以通過this.$route來訪問當(dāng)前路由。

  1. 在你的頁面中添加router-linkrouter-view標(biāo)簽
<template>
  <div>
    <router-link to="/key1">router-link:鏈接</router-link>

    <h3>router-view: 路由匹配到的組件顯示在下面</h3>
    <router-view></router-view>

  </div>
</template>
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容