Vue動態路由



Vue動態路由
1、不同路由傳值:動態路由

1、配置動態路由

routes: [
// 動態路徑參數 以冒號開頭
{ path: '/user/:id', component: User }
]


2、在對應的頁面

this.$route.params獲取動態路由的值
var aid=this.$route.params.aid;
this.$route.query //獲取get傳值 

//第一種跳轉方式
// this.$router.push({ path: 'news' })
// this.$router.push({ path: '/content/495' });


//另一種跳轉方式
// { path: '/news', component: News,name:'news' },
// router.push({ name: 'news', params: { userId: 123 }})
this.$router.push({ name: 'news'})

 

https://router.vuejs.org/


vue路由配置:


1.安裝

npm install vue-router --save / cnpm install vue-router --save


2、引入並 Vue.use(VueRouter) (main.js)

import VueRouter from 'vue-router'

Vue.use(VueRouter)


3、配置路由

 

1、創建組件 引入組件


2、定義路由 (建議復制s)

const routes = [
{ path: '/foo', component: Foo },
{ path: '/bar', component: Bar },
{ path: '*', redirect: '/home' } /*默認跳轉路由*/
]

3、實例化VueRouter

const router = new VueRouter({
routes // (縮寫)相當於 routes: routes
})

4、掛載


new Vue({
el: '#app',
router,
render: h => h(App)
})



5 、根組件的模板里面放上這句話 <router-view></router-view>

 


6、路由跳轉
<router-link to="/foo">Go to Foo</router-link>
<router-link to="/bar">Go to Bar</router-link>

 


免責聲明!

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



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