vue 路由的安裝及使用


安裝:

  1.cmd下輸入: npm install vue-router --save //安裝路由
  2.npm run dev //重新啟動

使用:
  1.在mian.js下引入,使用,配置路由

     import VueRouter from 'vue-router'
          Vue.use(VueRouter)

    const router(定義一個名字)=new VueRouter({

      routes:[
        {path:"(路由)",component:跳轉的位置},
        {path:"/",component:VueRouter}, //跳轉的位置要引進來(import VueRouter from 'vue-router')
        {path:"路由",name:'取個名字',component:跳轉的位置}, //輸入個name,用於綁定個名字
        {path:"/about(路由)",name:'取個名字',component:跳轉的位置,children:[
        {path:"/about/aboutour(路由)",name:'取個名字',component:跳轉的位置},
        {path:"/aboutour(路由)",name:'取個名字',component:跳轉的位置}]}, //二級路由
        {path:"(路由)",component:跳轉的位置,beforeEnter: (to, from, next) => {
           例如:alert('非登錄狀態禁止訪問頁面');
           next(false); //禁止跳轉到此頁面
        }},//路由獨享守衛 針對指定頁面 全局守衛也可以在此寫
        {path:"路由",name:'取個名字',components:{
          default:'原本要跳轉的',
          '取的名字':取的名字,
          '取的名字':取的名字
        }},//一個頁面想要另外一個頁面的某些東西 在原本頁面寫入<route-view name="取個名字"></route-view>
        {path:'*',redirect:'/'} //如果用戶輸入錯誤,默認展示頁
      ],
      mode:"history" //去掉后面的#號
   })

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM