Vue Router動態(tài)路由使用方法總結(jié)
作用:動態(tài)拼接一些路徑
語法:/user/:userId(:XX表示可以動態(tài)拼接,路徑可以渲染成/user/zhangsan或者/user/lisi等等)
1.通過router-link渲染的跳轉(zhuǎn)頁面
通過動態(tài)綁定to屬性后面跟著動態(tài)路由來跳轉(zhuǎn)頁面(:to="’/about/’+info")
Router-Index.js
import Vue from 'vue'; import VueRouter from 'vue-router'; Vue.use(VueRouter); const About = () => import('../views/About.vue'); const routes = [ { path: '/about/:info', component: About } ]; const router = new VueRouter({ routes, mode: 'history', linkActiveClass: 'active' }); export default router;
App.vue
<template> <div> <!-- 通過router-link動態(tài)路由傳參 --> <router-link :to="'/about/'+info">About</router-link> <router-view></router-view> </div> </template> <script> export default { name: 'App', data() { return { info: 'hello vuejs' } } } </script> <style> .active { color: #1890ff; } </style>
About.vue
<template> <div> <h2>About</h2> <!-- 可以通過($route.params.動態(tài)傳參)獲取數(shù)據(jù) --> <p>{{ $route.params.info }}</p> </div> </template> <script> export default { name: 'About' } </script> <style> </style>
2.通過普通標(biāo)簽渲染的跳轉(zhuǎn)頁面
通過this.$router.push(’/user/’ + this.userId)獲取數(shù)據(jù)
Router-Index.js
import Vue from 'vue'; import VueRouter from 'vue-router'; Vue.use(VueRouter); const User = () => import('../views/User.vue'); const routes = [ { path: '/user/:userId', component: User } ]; const router = new VueRouter({ routes, mode: 'history', linkActiveClass: 'active' }); export default router;
App.vue
<template> <div> <!-- 通過普通button動態(tài)路由傳參 --> <button @click="userClick">用戶</button> <router-view></router-view> </div> </template> <script> export default { name: 'App', data() { return { userId: 'zhangsan' } }, methods: { userClick() { this.$router.push('/user/' + this.userId); } } } </script> <style> .active { color: #1890ff; } </style>
User.vue
<template> <div> <h2>User</h2> <p>{{ $route.params.userId }}</p> </div> </template> <script> export default { name: 'User' } </script> <style> </style>
到此這篇關(guān)于Vue Router動態(tài)路由使用方法總結(jié)的文章就介紹到這了,更多相關(guān)Vue Router動態(tài)路由內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue項(xiàng)目開發(fā)實(shí)現(xiàn)父組件與子組件數(shù)據(jù)間的雙向綁定原理及適用場景
在 Vue.js 中,實(shí)現(xiàn)父組件與子組件數(shù)據(jù)之間的雙向綁定,可以通過以下幾種方式,下面我將介紹幾種常見的方法,并解釋它們的實(shí)現(xiàn)原理和適用場景,感興趣的朋友跟隨小編一起看看吧2024-12-12解決VMware中vmware-vmx.exe進(jìn)程無法關(guān)閉以及死機(jī)等問題
這篇文章主要介紹了解決VMware中vmware-vmx.exe進(jìn)程無法關(guān)閉以及死機(jī)等問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06vue對象或者數(shù)組中數(shù)據(jù)變化但是視圖沒有更新問題及解決
這篇文章主要介紹了vue對象或者數(shù)組中數(shù)據(jù)變化但是視圖沒有更新問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07